woocommerce-paypal-payments/modules/ppcp-api-client/src/Factory/PatchCollectionFactory.php
Anton Ukhanev d4c8282518 Use Composer modules and convert modules to PSR-4
PSR-4 is much more robust and predictable. But to do this,
a source root dir must be specified for every module.
This could be done in the root file, but this is not very modular.
Instead, now every module declares its own source root by using
the amazing Composer Merge Plugin. This approach allows each module
to also declare its own dependencies. Together, these changes allow
modules to be easily extractable to separate pacakges when the need
arises, and in general improves modularity significantly.
2021-08-26 11:01:20 +02:00

85 lines
2.3 KiB
PHP

<?php
/**
* The PatchCollection factory.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Patch;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PatchCollection;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
/**
* Class PatchCollectionFactory
*/
class PatchCollectionFactory {
/**
* Creates a Patch Collection by comparing two orders.
*
* @param Order $from The inital order.
* @param Order $to The target order.
*
* @return PatchCollection
*/
public function from_orders( Order $from, Order $to ): PatchCollection {
$all_patches = array();
$all_patches += $this->purchase_units( $from->purchase_units(), $to->purchase_units() );
return new PatchCollection( ...$all_patches );
}
/**
* Returns patches from purchase units diffs.
*
* @param PurchaseUnit[] $from The Purchase Units to start with.
* @param PurchaseUnit[] $to The Purchase Units to end with after patches where applied.
*
* @return Patch[]
*/
private function purchase_units( array $from, array $to ): array {
$patches = array();
$path = '/purchase_units';
foreach ( $to as $purchase_unit_to ) {
$needs_update = ! count(
array_filter(
$from,
static function ( PurchaseUnit $unit ) use ( $purchase_unit_to ): bool {
//phpcs:disable WordPress.PHP.StrictComparisons.LooseComparison
// Loose comparison needed to compare two objects.
return $unit == $purchase_unit_to;
//phpcs:enable WordPress.PHP.StrictComparisons.LooseComparison
}
)
);
$needs_update = true;
if ( ! $needs_update ) {
continue;
}
$purchase_unit_from = current(
array_filter(
$from,
static function ( PurchaseUnit $unit ) use ( $purchase_unit_to ): bool {
return $purchase_unit_to->reference_id() === $unit->reference_id();
}
)
);
$operation = $purchase_unit_from ? 'replace' : 'add';
$value = $purchase_unit_to->to_array();
$patches[] = new Patch(
$operation,
$path . "/@reference_id=='" . $purchase_unit_to->reference_id() . "'",
$value
);
}
return $patches;
}
}