Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

ProductAssociationContext   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 8
dl 0
loc 194
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A theStoreHasAProductAssociationType() 0 4 1
A itHasVariantNamedInAndIn() 0 11 2
A theStoreHasProductAssociationTypes() 0 6 2
A theProductHasAnAssociationWithProduct() 0 7 1
A theProductHasAnAssociationWithProducts() 0 7 1
A createProductAssociationType() 0 16 2
A createProductAssociation() 0 17 2
A addProductAssociationTypeTranslation() 0 12 1
A generateCodeFromName() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Behat\Service\SharedStorageInterface;
17
use Sylius\Component\Core\Model\ProductInterface;
18
use Sylius\Component\Product\Model\ProductAssociationInterface;
19
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
20
use Sylius\Component\Product\Model\ProductAssociationTypeTranslationInterface;
21
use Sylius\Component\Product\Repository\ProductAssociationTypeRepositoryInterface;
22
use Sylius\Component\Resource\Factory\FactoryInterface;
23
use Sylius\Component\Resource\Model\TranslationInterface;
24
use Sylius\Component\Resource\Repository\RepositoryInterface;
25
26
/**
27
 * @author Grzegorz Sadowski <[email protected]>
28
 */
29
final class ProductAssociationContext implements Context
30
{
31
    /**
32
     * @var SharedStorageInterface
33
     */
34
    private $sharedStorage;
35
36
    /**
37
     * @var FactoryInterface
38
     */
39
    private $productAssociationTypeFactory;
40
41
    /**
42
     * @var FactoryInterface
43
     */
44
    private $productAssociationTypeTranslationFactory;
45
46
    /**
47
     * @var FactoryInterface
48
     */
49
    private $productAssociationFactory;
50
51
    /**
52
     * @var ProductAssociationTypeRepositoryInterface
53
     */
54
    private $productAssociationTypeRepository;
55
56
    /**
57
     * @var RepositoryInterface
58
     */
59
    private $productAssociationRepository;
60
61
    /**
62
     * @var ObjectManager
63
     */
64
    private $objectManager;
65
66
    /**
67
     * @param SharedStorageInterface $sharedStorage
68
     * @param FactoryInterface $productAssociationTypeFactory
69
     * @param FactoryInterface $productAssociationTypeTranslationFactory
70
     * @param FactoryInterface $productAssociationFactory
71
     * @param ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository
72
     * @param RepositoryInterface $productAssociationRepository
73
     * @param ObjectManager $objectManager
74
     */
75
    public function __construct(
76
        SharedStorageInterface $sharedStorage,
77
        FactoryInterface $productAssociationTypeFactory,
78
        FactoryInterface $productAssociationTypeTranslationFactory,
79
        FactoryInterface $productAssociationFactory,
80
        ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository,
81
        RepositoryInterface $productAssociationRepository,
82
        ObjectManager $objectManager
83
    ) {
84
        $this->sharedStorage = $sharedStorage;
85
        $this->productAssociationTypeFactory = $productAssociationTypeFactory;
86
        $this->productAssociationTypeTranslationFactory = $productAssociationTypeTranslationFactory;
87
        $this->productAssociationFactory = $productAssociationFactory;
88
        $this->productAssociationTypeRepository = $productAssociationTypeRepository;
89
        $this->productAssociationRepository = $productAssociationRepository;
90
        $this->objectManager = $objectManager;
91
    }
92
93
    /**
94
     * @Given the store has (also) a product association type :name
95
     * @Given the store has (also) a product association type :name with a code :code
96
     */
97
    public function theStoreHasAProductAssociationType($name, $code = null)
98
    {
99
        $this->createProductAssociationType($name, $code);
100
    }
101
102
    /**
103
     * @Given /^the store has(?:| also) a product association type named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/
104
     */
105
    public function itHasVariantNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale)
106
    {
107
        $productAssociationType = $this->createProductAssociationType($firstName);
108
109
        $names = [$firstName => $firstLocale, $secondName => $secondLocale];
110
        foreach ($names as $name => $locale) {
111
            $this->addProductAssociationTypeTranslation($productAssociationType, $name, $locale);
112
        }
113
114
        $this->objectManager->flush();
115
    }
116
117
    /**
118
     * @Given the store has :firstName and :secondName product association types
119
     */
120
    public function theStoreHasProductAssociationTypes(...$names)
121
    {
122
        foreach ($names as $name) {
123
            $this->createProductAssociationType($name);
124
        }
125
    }
126
127
    /**
128
     * @Given /^the (product "[^"]+") has(?:| also) an (association "[^"]+") with (product "[^"]+")$/
129
     */
130
    public function theProductHasAnAssociationWithProduct(
131
        ProductInterface $product,
132
        ProductAssociationTypeInterface $productAssociationType,
133
        ProductInterface $associatedProduct
134
    ) {
135
        $this->createProductAssociation($product, $productAssociationType, [$associatedProduct]);
136
    }
137
138
    /**
139
     * @Given /^the (product "[^"]+") has(?:| also) an (association "[^"]+") with (products "[^"]+" and "[^"]+")$/
140
     */
141
    public function theProductHasAnAssociationWithProducts(
142
        ProductInterface $product,
143
        ProductAssociationTypeInterface $productAssociationType,
144
        array $associatedProducts
145
    ) {
146
        $this->createProductAssociation($product, $productAssociationType, $associatedProducts);
147
    }
148
149
    /**
150
     * @param string $name
151
     * @param string|null $code
152
     *
153
     * @return ProductAssociationTypeInterface
154
     */
155
    private function createProductAssociationType($name, $code = null)
156
    {
157
        if (null === $code) {
158
            $code = $this->generateCodeFromName($name);
159
        }
160
161
        /** @var ProductAssociationTypeInterface $productAssociationType */
162
        $productAssociationType = $this->productAssociationTypeFactory->createNew();
163
        $productAssociationType->setCode($code);
164
        $productAssociationType->setName($name);
165
166
        $this->productAssociationTypeRepository->add($productAssociationType);
167
        $this->sharedStorage->set('product_association_type', $productAssociationType);
168
169
        return $productAssociationType;
170
    }
171
172
    /**
173
     * @param ProductInterface $product
174
     * @param ProductAssociationTypeInterface $productAssociationType
175
     * @param array $associatedProducts
176
     */
177
    private function createProductAssociation(
178
        ProductInterface $product,
179
        ProductAssociationTypeInterface $productAssociationType,
180
        array $associatedProducts
181
    ) {
182
        /** @var ProductAssociationInterface $productAssociation */
183
        $productAssociation = $this->productAssociationFactory->createNew();
184
        $productAssociation->setType($productAssociationType);
185
186
        foreach ($associatedProducts as $associatedProduct) {
187
            $productAssociation->addAssociatedProduct($associatedProduct);
188
        }
189
190
        $product->addAssociation($productAssociation);
191
192
        $this->productAssociationRepository->add($productAssociation);
193
    }
194
195
    /**
196
     * @param ProductAssociationTypeInterface $productAssociationType
197
     * @param string $name
198
     * @param string $locale
199
     */
200
    private function addProductAssociationTypeTranslation(
201
        ProductAssociationTypeInterface $productAssociationType,
202
        $name,
203
        $locale
204
    ) {
205
        /** @var ProductAssociationTypeTranslationInterface|TranslationInterface $translation */
206
        $translation = $this->productAssociationTypeTranslationFactory->createNew();
207
        $translation->setLocale($locale);
0 ignored issues
show
Bug introduced by
The method setLocale does only exist in Sylius\Component\Resourc...el\TranslationInterface, but not in Sylius\Component\Product...ypeTranslationInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
208
        $translation->setName($name);
0 ignored issues
show
Bug introduced by
The method setName does only exist in Sylius\Component\Product...ypeTranslationInterface, but not in Sylius\Component\Resourc...el\TranslationInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
209
210
        $productAssociationType->addTranslation($translation);
0 ignored issues
show
Bug introduced by
It seems like $translation defined by $this->productAssociatio...ionFactory->createNew() on line 206 can also be of type object<Sylius\Component\...peTranslationInterface>; however, Sylius\Component\Resourc...rface::addTranslation() does only seem to accept object<Sylius\Component\...l\TranslationInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
211
    }
212
213
    /**
214
     * @param string $name
215
     *
216
     * @return string
217
     */
218
    private function generateCodeFromName($name)
219
    {
220
        return str_replace([' ', '-'], '_', strtolower($name));
221
    }
222
}
223