Completed
Push — master ( 51c17f...9ad827 )
by Fabian
14s queued 10s
created

OrderFixtureRollback   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 19 2
A create() 0 11 2
A __construct() 0 10 1
1
<?php
2
3
namespace TddWizard\Fixtures\Sales;
4
5
use Magento\Catalog\Api\ProductRepositoryInterface;
6
use Magento\Customer\Api\CustomerRepositoryInterface;
7
use Magento\Framework\Exception\LocalizedException;
8
use Magento\Framework\Exception\NoSuchEntityException;
9
use Magento\Framework\ObjectManagerInterface;
10
use Magento\Framework\Registry;
11
use Magento\Sales\Api\Data\OrderItemInterface;
12
use Magento\Sales\Api\OrderRepositoryInterface;
13
14
class OrderFixtureRollback
15
{
16
    /**
17
     * @var Registry
18
     */
19
    private $registry;
20
21
    /**
22
     * @var OrderRepositoryInterface
23
     */
24
    private $orderRepository;
25
26
    /**
27
     * @var CustomerRepositoryInterface
28
     */
29
    private $customerRepository;
30
31
    /**
32
     * @var ProductRepositoryInterface
33
     */
34
    private $productRepository;
35
36
    public function __construct(
37
        Registry $registry,
38
        OrderRepositoryInterface $orderRepository,
39
        CustomerRepositoryInterface $customerRepository,
40
        ProductRepositoryInterface $productRepository
41
    ) {
42
        $this->registry = $registry;
43
        $this->orderRepository = $orderRepository;
44
        $this->customerRepository = $customerRepository;
45
        $this->productRepository = $productRepository;
46
    }
47
48
    public static function create(ObjectManagerInterface $objectManager = null)
49
    {
50
        if ($objectManager === null) {
51
            $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
0 ignored issues
show
Bug introduced by
The type Magento\TestFramework\Helper\Bootstrap 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...
52
        }
53
54
        return new self(
55
            $objectManager->get(Registry::class),
56
            $objectManager->get(OrderRepositoryInterface::class),
57
            $objectManager->get(CustomerRepositoryInterface::class),
58
            $objectManager->get(ProductRepositoryInterface::class)
59
        );
60
    }
61
62
    /**
63
     * Roll back orders with associated customers and products.
64
     *
65
     * @param OrderFixture ...$orderFixtures
66
     * @throws LocalizedException
67
     * @throws NoSuchEntityException
68
     */
69
    public function execute(OrderFixture ...$orderFixtures)
70
    {
71
        $this->registry->unregister('isSecureArea');
72
        $this->registry->register('isSecureArea', true);
73
74
        foreach ($orderFixtures as $orderFixture) {
75
            $orderItems = $this->orderRepository->get($orderFixture->getId())->getItems();
76
77
            $this->orderRepository->deleteById($orderFixture->getId());
0 ignored issues
show
Bug introduced by
The method deleteById() does not exist on Magento\Sales\Api\OrderRepositoryInterface. Did you maybe mean delete()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $this->orderRepository->/** @scrutinizer ignore-call */ 
78
                                    deleteById($orderFixture->getId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
            $this->customerRepository->deleteById($orderFixture->getCustomerId());
79
            array_walk(
80
                $orderItems,
81
                function (OrderItemInterface $orderItem) {
82
                    $this->productRepository->deleteById($orderItem->getSku());
83
                }
84
            );
85
        }
86
87
        $this->registry->unregister('isSecureArea');
88
    }
89
}
90