Completed
Push — master ( 79fbfe...1587e3 )
by Fabian
16s
created

CategoryBuilder::withUrlKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TddWizard\Fixtures\Catalog;
4
5
use Magento\Catalog\Api\CategoryLinkRepositoryInterface;
6
use Magento\Catalog\Api\CategoryRepositoryInterface;
7
use Magento\Catalog\Api\Data\CategoryInterface;
8
use Magento\Catalog\Api\Data\CategoryProductLinkInterface;
9
use Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Catalog\Api\Data...uctLinkInterfaceFactory 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...
10
use Magento\Framework\ObjectManagerInterface;
11
12
class CategoryBuilder
13
{
14
    /**
15
     * @var CategoryInterface
16
     */
17
    private $category;
18
19
    /**
20
     * @var CategoryRepositoryInterface
21
     */
22
    private $categoryRepository;
23
24
    /**
25
     * @var CategoryLinkRepositoryInterface
26
     */
27
    private $categoryLinkRepository;
28
29
    /**
30
     * @var CategoryProductLinkInterface
31
     */
32
    private $productLinkFactory;
33
34
    /**
35
     * @var int[]
36
     */
37
    private $skus = [];
38
39
    public function __construct(
40
        CategoryRepositoryInterface $categoryRepository,
41
        CategoryLinkRepositoryInterface $categoryLinkRepository,
42
        CategoryProductLinkInterfaceFactory $productLinkFactory,
43
        CategoryInterface $category,
44
        array $skus
45
    ) {
46
        $this->categoryRepository = $categoryRepository;
47
        $this->categoryLinkRepository = $categoryLinkRepository;
48
        $this->category = $category;
49
        $this->skus = $skus;
50
        $this->productLinkFactory = $productLinkFactory;
51
    }
52
53
    public static function topLevelCategory(ObjectManagerInterface $objectManager = null) : CategoryBuilder
54
    {
55
        if ($objectManager === null) {
56
            $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...
57
        }
58
        /** @var CategoryInterface $category */
59
        $category = $objectManager->create(CategoryInterface::class);
60
61
        $category->setName('Top Level Category');
62
        $category->setIsActive(true);
63
64
        return new self(
65
            $objectManager->create(CategoryRepositoryInterface::class),
66
            $objectManager->create(CategoryLinkRepositoryInterface::class),
67
            $objectManager->create(CategoryProductLinkInterfaceFactory::class),
68
            $category,
69
            []
70
        );
71
    }
72
73
    public static function childCategoryOf(
74
        CategoryFixture $parent,
75
        ObjectManagerInterface $objectManager = null
76
    ): CategoryBuilder
77
    {
78
        if ($objectManager === null) {
79
            $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
80
        }
81
        /** @var CategoryInterface $category */
82
        $category = $objectManager->create(CategoryInterface::class);
83
84
        $category->setName('Child Category');
85
        $category->setIsActive(true);
86
        $category->setParentId($parent->getId());
87
88
        return new self(
89
            $objectManager->create(CategoryRepositoryInterface::class),
90
            $objectManager->create(CategoryLinkRepositoryInterface::class),
91
            $objectManager->create(CategoryProductLinkInterfaceFactory::class),
92
            $category,
93
            []
94
        );
95
    }
96
97
    /**
98
     * Assigns products by sku. The keys of the array will be used for the sort position
99
     *
100
     * @param string[] $skus
101
     * @return CategoryBuilder
102
     */
103
    public function withProducts(array $skus) : CategoryBuilder
104
    {
105
        $builder = clone $this;
106
        $builder->skus = $skus;
0 ignored issues
show
Documentation Bug introduced by
It seems like $skus of type string[] is incompatible with the declared type integer[] of property $skus.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
107
        return $builder;
108
    }
109
110
    public function withDescription(string $description) : CategoryBuilder
111
    {
112
        $builder = clone $this;
113
        $builder->category->setCustomAttribute('description', $description);
114
        return $builder;
115
    }
116
117
    public function withName(string $name) : CategoryBuilder
118
    {
119
        $builder = clone $this;
120
        $builder->category->setName($name);
121
        return $builder;
122
    }
123
124
    public function withUrlKey(string $urlKey) : CategoryBuilder
125
    {
126
        $builder = clone $this;
127
        $builder->category->setData('url_key', $urlKey);
128
        return $builder;
129
    }
130
131
    public function withIsActive(bool $isActive) : CategoryBuilder
132
    {
133
        $builder = clone $this;
134
        $builder->category->setIsActive($isActive);
135
        return $builder;
136
    }
137
138
    public function __clone()
139
    {
140
        $this->category = clone $this->category;
141
    }
142
143
    public function build() : CategoryInterface
144
    {
145
        $builder = clone $this;
146
        if (!$builder->category->getData('url_key')) {
147
            $builder->category->setData('url_key', sha1(uniqid('', true)));
148
        }
149
150
        $category = $builder->categoryRepository->save($builder->category);
151
152
        foreach ($builder->skus as $position => $sku) {
153
            /** @var CategoryProductLinkInterface $productLink */
154
            $productLink = $builder->productLinkFactory->create();
0 ignored issues
show
Bug introduced by
The method create() does not exist on Magento\Catalog\Api\Data...oryProductLinkInterface. ( Ignorable by Annotation )

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

154
            /** @scrutinizer ignore-call */ 
155
            $productLink = $builder->productLinkFactory->create();

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...
155
            $productLink->setSku($sku);
156
            $productLink->setPosition($position);
157
            $productLink->setCategoryId($category->getId());
158
            $builder->categoryLinkRepository->save($productLink);
159
        }
160
        return $category;
161
    }
162
}