Passed
Pull Request — master (#34)
by
unknown
07:48
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\ObjectManagerInterface;
9
use Magento\Framework\Registry;
10
use Magento\Sales\Api\Data\OrderItemInterface;
11
use Magento\Sales\Api\OrderRepositoryInterface;
12
13
class OrderFixtureRollback
14
{
15
    /**
16
     * @var Registry
17
     */
18
    private $registry;
19
20
    /**
21
     * @var OrderRepositoryInterface
22
     */
23
    private $orderRepository;
24
25
    /**
26
     * @var CustomerRepositoryInterface
27
     */
28
    private $customerRepository;
29
30
    /**
31
     * @var ProductRepositoryInterface
32
     */
33
    private $productRepository;
34
35
    public function __construct(
36
        Registry $registry,
37
        OrderRepositoryInterface $orderRepository,
38
        CustomerRepositoryInterface $customerRepository,
39
        ProductRepositoryInterface $productRepository
40
    ) {
41
        $this->registry = $registry;
42
        $this->orderRepository = $orderRepository;
43
        $this->customerRepository = $customerRepository;
44
        $this->productRepository = $productRepository;
45
    }
46
47
    public static function create(ObjectManagerInterface $objectManager = null)
48
    {
49
        if ($objectManager === null) {
50
            $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...
51
        }
52
53
        return new self(
54
            $objectManager->get(Registry::class),
55
            $objectManager->get(OrderRepositoryInterface::class),
56
            $objectManager->get(CustomerRepositoryInterface::class),
57
            $objectManager->get(ProductRepositoryInterface::class)
58
        );
59
    }
60
61
    /**
62
     * Roll back orders with associated customers and products.
63
     *
64
     * @param OrderFixture ...$orderFixtures
65
     * @throws \Magento\Framework\Exception\LocalizedException
66
     * @throws \Magento\Framework\Exception\NoSuchEntityException
67
     */
68
    public function execute(OrderFixture ...$orderFixtures)
69
    {
70
        $this->registry->unregister('isSecureArea');
71
        $this->registry->register('isSecureArea', true);
72
73
        foreach ($orderFixtures as $orderFixture) {
74
            $orderItems = $this->orderRepository->get($orderFixture->getId())->getItems();
75
76
            $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

76
            $this->orderRepository->/** @scrutinizer ignore-call */ 
77
                                    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...
77
            $this->customerRepository->deleteById($orderFixture->getCustomerId());
78
            array_walk(
79
                $orderItems,
80
                function (OrderItemInterface $orderItem) {
81
                    $this->productRepository->deleteById($orderItem->getSku());
82
                }
83
            );
84
        }
85
86
        $this->registry->unregister('isSecureArea');
87
    }
88
}
89