Completed
Push — master ( e9a7f6...2665f3 )
by Kamil
46:03 queued 28:24
created

ProductAttributeContext::saveProductAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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 Sylius\Behat\Service\SharedStorageInterface;
16
use Sylius\Component\Attribute\Factory\AttributeFactoryInterface;
17
use Sylius\Component\Attribute\Repository\AttributeRepositoryInterface;
18
use Sylius\Component\Core\Formatter\StringInflector;
19
use Sylius\Component\Product\Model\ProductAttributeInterface;
20
21
/**
22
 * @author Anna Walasek <[email protected]>
23
 */
24
final class ProductAttributeContext implements Context
25
{
26
    /**
27
     * @var SharedStorageInterface
28
     */
29
    private $sharedStorage;
30
31
    /**
32
     * @var AttributeFactoryInterface
33
     */
34
    private $productAttributeFactory;
35
36
    /**
37
     * @var AttributeRepositoryInterface
38
     */
39
    private $productAttributeRepository;
40
41
    /**
42
     * @param SharedStorageInterface $sharedStorage
43
     * @param AttributeRepositoryInterface $productAttributeRepository
44
     * @param AttributeFactoryInterface $productAttributeFactory
45
     */
46
    public function __construct(
47
        SharedStorageInterface $sharedStorage,
48
        AttributeRepositoryInterface $productAttributeRepository,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAttributeRepository 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...
49
        AttributeFactoryInterface $productAttributeFactory
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAttributeFactory 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...
50
    ) {
51
        $this->sharedStorage = $sharedStorage;
52
        $this->productAttributeRepository = $productAttributeRepository;
53
        $this->productAttributeFactory = $productAttributeFactory;
54
    }
55
56
    /**
57
     * @Given the store has a :type product attribute :name with code :code
58
     */
59
    public function theStoreHasAProductAttributeWithCode($type, $name, $code)
60
    {
61
        $productAttribute = $this->createProductAttribute($type, $name, $code);
62
63
        $this->saveProductAttribute($productAttribute);
64
    }
65
66
    /**
67
     * @Given the store( also) has a :type product attribute :name at position :position
68
     */
69
    public function theStoreHasAProductAttributeWithPosition($type, $name, $position)
70
    {
71
        $productAttribute = $this->createProductAttribute($type, $name);
72
        $productAttribute->setPosition($position);
73
74
        $this->saveProductAttribute($productAttribute);
75
    }
76
77
    /**
78
     * @Given the store( also) has a :type product attribute :name
79
     */
80
    public function theStoreHasATextProductAttribute($type, $name)
81
    {
82
        $productAttribute = $this->createProductAttribute($type, $name);
83
84
        $this->saveProductAttribute($productAttribute);
85
    }
86
87
    /**
88
     * @param string $type
89
     * @param string $name
90
     * @param string|null $code
91
     *
92
     * @return ProductAttributeInterface
93
     */
94
    private function createProductAttribute($type, $name, $code = null)
95
    {
96
        $productAttribute = $this->productAttributeFactory->createTyped($type);
97
98
        if (null === $code) {
99
            $code = StringInflector::nameToUppercaseCode($name);
100
        }
101
102
        $productAttribute->setCode($code);
103
        $productAttribute->setName($name);
104
105
        return $productAttribute;
106
    }
107
108
    /**
109
     * @param ProductAttributeInterface $productAttribute
110
     */
111
    private function saveProductAttribute(ProductAttributeInterface $productAttribute)
112
    {
113
        $this->productAttributeRepository->add($productAttribute);
114
        $this->sharedStorage->set('product_attribute', $productAttribute);
115
    }
116
}
117