Passed
Pull Request — master (#51)
by Aleksey
12:12
created

CancelAndRecalculateCommandByOrderPlugin::run()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 21
rs 9.8666
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\Payone\Communication\Plugin\Oms\Command;
9
10
use Orm\Zed\Sales\Persistence\SpySalesOrder;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
12
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;
13
use Spryker\Zed\Oms\Dependency\Plugin\Command\CommandByOrderInterface;
14
15
/**
16
 * @method \SprykerEco\Zed\Payone\Business\PayoneFacadeInterface getFacade()
17
 * @method \SprykerEco\Zed\Payone\Communication\PayoneCommunicationFactory getFactory()
18
 * @method \SprykerEco\Zed\Payone\PayoneConfig getConfig()
19
 */
20
class CancelAndRecalculateCommandByOrderPlugin extends AbstractPlugin implements CommandByOrderInterface
21
{
22
    /**
23
     * {@inheritDoc}
24
     * - Cancels amount of all other items.
25
     *
26
     * @api
27
     *
28
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $orderItems
29
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
30
     * @param \Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject $data
31
     *
32
     * @return array
33
     */
34
    public function run(array $orderItems, SpySalesOrder $orderEntity, ReadOnlyArrayObject $data)
35
    {
36
        $orderTransfer = $this->getFactory()
37
            ->getSalesFacade()
38
            ->getOrderByIdSalesOrder($orderEntity->getIdSalesOrder());
39
40
        $orderItemIds = [];
41
        foreach ($orderItems as $orderItem) {
42
            $orderItemIds[] = $orderItem->getIdSalesOrderItem();
43
        }
44
45
        foreach ($orderTransfer->getItems() as $itemTransfer) {
46
            if (in_array($itemTransfer->getIdSalesOrderItem(), $orderItemIds)) {
47
                $itemTransfer->setCanceledAmount($itemTransfer->getSumPriceToPayAggregation());
48
            }
49
        }
50
51
        $orderTransfer = $this->getFactory()->getCalculationFacade()->recalculateOrder($orderTransfer);
52
        $this->getFactory()->getSalesFacade()->updateOrder($orderTransfer, $orderEntity->getIdSalesOrder());
53
54
        return [];
55
    }
56
}
57