Completed
Push — master ( 13b24b...9955bf )
by Fabian
04:09
created

CategoryBuilder::withName()   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 9.4285
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)
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
    /**
74
     * Assigns products by sku. The keys of the array will be used for the sort position
75
     *
76
     * @param array $skus
77
     * @return CategoryBuilder
78
     */
79
    public function withProducts(array $skus)
80
    {
81
        $builder = clone $this;
82
        $builder->skus = $skus;
83
        return $builder;
84
    }
85
86
    public function withDescription($description)
87
    {
88
        $builder = clone $this;
89
        $builder->category->setCustomAttribute('description', $description);
90
        return $builder;
91
    }
92
93
    public function withName($name)
94
    {
95
        $builder = clone $this;
96
        $builder->category->setName($name);
97
        return $builder;
98
    }
99
100
    public function __clone()
101
    {
102
        $this->category = clone $this->category;
103
    }
104
105
    public function build() : CategoryInterface
106
    {
107
        $builder = clone $this;
108
        if (!$builder->category->getData('url_key')) {
109
            $builder->category->setData('url_key', sha1(uniqid('', true)));
110
        }
111
112
        $category = $builder->categoryRepository->save($builder->category);
113
114
        foreach ($builder->skus as $position => $sku) {
115
            /** @var CategoryProductLinkInterface $productLink */
116
            $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

116
            /** @scrutinizer ignore-call */ 
117
            $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...
117
            $productLink->setSku($sku);
118
            $productLink->setPosition($position);
119
            $productLink->setCategoryId($category->getId());
120
            $builder->categoryLinkRepository->save($productLink);
121
        }
122
        return $category;
123
    }
124
}