Completed
Push — master ( 3ff7e6...cf5edc )
by Kamil
96:30 queued 63:14
created

createProductAssociation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 3
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 Sylius\Behat\Service\SharedStorageInterface;
16
use Sylius\Component\Association\Model\AssociationTypeInterface;
17
use Sylius\Component\Core\Model\ProductInterface;
18
use Sylius\Component\Product\Model\ProductAssociationInterface;
19
use Sylius\Component\Resource\Factory\FactoryInterface;
20
use Sylius\Component\Resource\Repository\RepositoryInterface;
21
22
/**
23
 * @author Grzegorz Sadowski <[email protected]>
24
 */
25
final class ProductAssociationContext implements Context
26
{
27
    /**
28
     * @var SharedStorageInterface
29
     */
30
    private $sharedStorage;
31
32
    /**
33
     * @var FactoryInterface
34
     */
35
    private $productAssociationTypeFactory;
36
37
    /**
38
     * @var FactoryInterface
39
     */
40
    private $productAssociationFactory;
41
42
    /**
43
     * @var RepositoryInterface
44
     */
45
    private $productAssociationTypeRepository;
46
47
    /**
48
     * @var RepositoryInterface
49
     */
50
    private $productAssociationRepository;
51
52
    /**
53
     * @param SharedStorageInterface $sharedStorage
54
     * @param FactoryInterface $productAssociationTypeFactory
55
     * @param FactoryInterface $productAssociationFactory
56
     * @param RepositoryInterface $productAssociationTypeRepository
57
     * @param RepositoryInterface $productAssociationRepository
58
     */
59
    public function __construct(
60
        SharedStorageInterface $sharedStorage,
61
        FactoryInterface $productAssociationTypeFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAssociationTypeFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
62
        FactoryInterface $productAssociationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAssociationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
63
        RepositoryInterface $productAssociationTypeRepository,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAssociationTypeRepository exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
64
        RepositoryInterface $productAssociationRepository
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAssociationRepository exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
65
    ) {
66
        $this->sharedStorage = $sharedStorage;
67
        $this->productAssociationTypeFactory = $productAssociationTypeFactory;
68
        $this->productAssociationFactory = $productAssociationFactory;
69
        $this->productAssociationTypeRepository = $productAssociationTypeRepository;
70
        $this->productAssociationRepository = $productAssociationRepository;
71
    }
72
73
    /**
74
     * @Given the store has (also) a product association type :name
75
     * @Given the store has (also) a product association type :name with a code :code
76
     */
77
    public function theStoreHasAProductAssociationType($name, $code = null)
78
    {
79
        $this->createProductAssociationType($name, $code);
80
    }
81
82
    /**
83
     * @Given /^the (product "[^"]+") has(?:| also) an (association "[^"]+") with (products "[^"]+" and "[^"]+")$/
84
     */
85
    public function theProductHasAnAssociationWithProducts(
86
        ProductInterface $product,
87
        AssociationTypeInterface $productAssociationType,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAssociationType exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
88
        array $products
89
    ) {
90
        $this->createProductAssociation($product, $productAssociationType, $products);
91
    }
92
93
    /**
94
     * @param string $name
95
     * @param string|null $code
96
     */
97
    private function createProductAssociationType($name, $code = null)
98
    {
99
        if (null === $code) {
100
            $code = $this->generateCodeFromName($name);
101
        }
102
103
        /** @var AssociationTypeInterface $productAssociationType */
104
        $productAssociationType = $this->productAssociationTypeFactory->createNew();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAssociationType exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
105
        $productAssociationType->setCode($code);
106
        $productAssociationType->setName($name);
107
108
        $this->productAssociationTypeRepository->add($productAssociationType);
109
        $this->sharedStorage->set('product_association_type', $productAssociationType);
110
    }
111
112
    /**
113
     * @param ProductInterface $product
114
     * @param AssociationTypeInterface $productAssociationType
115
     * @param array $associatedProducts
116
     */
117
    private function createProductAssociation(
118
        ProductInterface $product,
119
        AssociationTypeInterface $productAssociationType,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAssociationType exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
120
        array $associatedProducts
121
    ) {
122
        /** @var ProductAssociationInterface $productAssociation */
123
        $productAssociation = $this->productAssociationFactory->createNew();
124
        $productAssociation->setType($productAssociationType);
125
126
        foreach ($associatedProducts as $associatedProduct) {
127
            $productAssociation->addAssociatedObject($associatedProduct);
128
        }
129
130
        $product->addAssociation($productAssociation);
131
132
        $this->productAssociationRepository->add($productAssociation);
133
    }
134
135
    /**
136
     * @param string $name
137
     *
138
     * @return string
139
     */
140
    private function generateCodeFromName($name)
141
    {
142
        return str_replace([' ', '-'], '_', strtolower($name));
143
    }
144
}
145