Completed
Push — master ( 7d6b3f...2df636 )
by Fabian
14s queued 10s
created

OrderFixtureRollback   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 72
ccs 25
cts 25
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 19 2
A create() 0 9 1
A __construct() 0 10 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Sales;
5
6
use Magento\Catalog\Api\ProductRepositoryInterface;
7
use Magento\Customer\Api\CustomerRepositoryInterface;
8
use Magento\Framework\Exception\LocalizedException;
9
use Magento\Framework\Exception\NoSuchEntityException;
10
use Magento\Framework\Registry;
11
use Magento\Sales\Api\Data\OrderItemInterface;
12
use Magento\Sales\Api\OrderRepositoryInterface;
13
use Magento\Sales\Model\OrderRepository;
14
use Magento\TestFramework\Helper\Bootstrap;
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...
15
16
/**
17
 * @internal Use OrderFixture::rollback() or OrderFixturePool::rollback() instead
18
 */
19
class OrderFixtureRollback
20
{
21
    /**
22
     * @var Registry
23
     */
24
    private $registry;
25
26
    /**
27
     * @var OrderRepository
28
     */
29
    private $orderRepository;
30
31
    /**
32
     * @var CustomerRepositoryInterface
33
     */
34
    private $customerRepository;
35
36
    /**
37
     * @var ProductRepositoryInterface
38
     */
39
    private $productRepository;
40
41 16
    public function __construct(
42
        Registry $registry,
43
        OrderRepository $orderRepository,
44
        CustomerRepositoryInterface $customerRepository,
45
        ProductRepositoryInterface $productRepository
46
    ) {
47 16
        $this->registry = $registry;
48 16
        $this->orderRepository = $orderRepository;
49 16
        $this->customerRepository = $customerRepository;
50 16
        $this->productRepository = $productRepository;
51 16
    }
52
53 16
    public static function create(): OrderFixtureRollback
54
    {
55 16
        $objectManager = Bootstrap::getObjectManager();
56
57 16
        return new self(
58 16
            $objectManager->get(Registry::class),
59 16
            $objectManager->get(OrderRepositoryInterface::class),
60 16
            $objectManager->get(CustomerRepositoryInterface::class),
61 16
            $objectManager->get(ProductRepositoryInterface::class)
62
        );
63
    }
64
65
    /**
66
     * Roll back orders with associated customers and products.
67
     *
68
     * @param OrderFixture ...$orderFixtures
69
     * @throws LocalizedException
70
     * @throws NoSuchEntityException
71
     */
72 16
    public function execute(OrderFixture ...$orderFixtures): void
73
    {
74 16
        $this->registry->unregister('isSecureArea');
0 ignored issues
show
Deprecated Code introduced by
The function Magento\Framework\Registry::unregister() has been deprecated: 102.0.0 ( Ignorable by Annotation )

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

74
        /** @scrutinizer ignore-deprecated */ $this->registry->unregister('isSecureArea');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
75 16
        $this->registry->register('isSecureArea', true);
0 ignored issues
show
Deprecated Code introduced by
The function Magento\Framework\Registry::register() has been deprecated: 102.0.0 ( Ignorable by Annotation )

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

75
        /** @scrutinizer ignore-deprecated */ $this->registry->register('isSecureArea', true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
77 16
        foreach ($orderFixtures as $orderFixture) {
78 16
            $orderItems = $this->orderRepository->get($orderFixture->getId())->getItems();
79
80 16
            $this->orderRepository->deleteById($orderFixture->getId());
81 16
            $this->customerRepository->deleteById($orderFixture->getCustomerId());
82 16
            array_walk(
83 16
                $orderItems,
84
                function (OrderItemInterface $orderItem) {
85 16
                    $this->productRepository->deleteById($orderItem->getSku());
86 16
                }
87
            );
88
        }
89
90 16
        $this->registry->unregister('isSecureArea');
0 ignored issues
show
Deprecated Code introduced by
The function Magento\Framework\Registry::unregister() has been deprecated: 102.0.0 ( Ignorable by Annotation )

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

90
        /** @scrutinizer ignore-deprecated */ $this->registry->unregister('isSecureArea');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91 16
    }
92
}
93