Completed
Push — symfony3 ( a82edd...ec9673 )
by Kamil
17:27
created

TaxonomyContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 17
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Behat\Mink\Element\NodeElement;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Sylius\Component\Core\Formatter\StringInflector;
18
use Sylius\Component\Core\Model\ProductInterface;
19
use Sylius\Component\Core\Model\TaxonInterface;
20
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Model\TranslationInterface;
23
use Sylius\Component\Resource\Repository\RepositoryInterface;
24
use Sylius\Component\Taxonomy\Generator\TaxonSlugGeneratorInterface;
25
use Symfony\Component\HttpFoundation\File\UploadedFile;
26
27
/**
28
 * @author Mateusz Zalewski <[email protected]>
29
 */
30
final class TaxonomyContext implements Context
31
{
32
    /**
33
     * @var RepositoryInterface
34
     */
35
    private $taxonRepository;
36
37
    /**
38
     * @var FactoryInterface
39
     */
40
    private $taxonFactory;
41
42
    /**
43
     * @var FactoryInterface
44
     */
45
    private $taxonTranslationFactory;
46
47
    /**
48
     * @var FactoryInterface
49
     */
50
    private $taxonImageFactory;
51
52
    /**
53
     * @var ObjectManager
54
     */
55
    private $objectManager;
56
57
    /**
58
     * @var ImageUploaderInterface
59
     */
60
    private $imageUploader;
61
62
    /**
63
     * @var TaxonSlugGeneratorInterface
64
     */
65
    private $taxonSlugGenerator;
66
67
    /**
68
     * @var array
69
     */
70
    private $minkParameters;
71
72
    /**
73
     * @param RepositoryInterface $taxonRepository
74
     * @param FactoryInterface $taxonFactory
75
     * @param FactoryInterface $taxonTranslationFactory
76
     * @param FactoryInterface $taxonImageFactory
77
     * @param ObjectManager $objectManager
78
     * @param ImageUploaderInterface $imageUploader
79
     * @param TaxonSlugGeneratorInterface $taxonSlugGenerator
80
     * @param array $minkParameters
81
     */
82
    public function __construct(
83
        RepositoryInterface $taxonRepository,
84
        FactoryInterface $taxonFactory,
85
        FactoryInterface $taxonTranslationFactory,
86
        FactoryInterface $taxonImageFactory,
87
        ObjectManager $objectManager,
88
        ImageUploaderInterface $imageUploader,
89
        TaxonSlugGeneratorInterface $taxonSlugGenerator,
90
        array $minkParameters
91
    ) {
92
        $this->taxonRepository = $taxonRepository;
93
        $this->taxonFactory = $taxonFactory;
94
        $this->taxonTranslationFactory = $taxonTranslationFactory;
95
        $this->taxonImageFactory = $taxonImageFactory;
96
        $this->objectManager = $objectManager;
97
        $this->imageUploader = $imageUploader;
98
        $this->taxonSlugGenerator = $taxonSlugGenerator;
99
        $this->minkParameters = $minkParameters;
100
    }
101
102
    /**
103
     * @Given the store has :firstTaxonName taxonomy
104
     * @Given the store classifies its products as :firstTaxonName
105
     * @Given the store classifies its products as :firstTaxonName and :secondTaxonName
106
     * @Given the store classifies its products as :firstTaxonName, :secondTaxonName and :thirdTaxonName
107
     * @Given the store classifies its products as :firstTaxonName, :secondTaxonName, :thirdTaxonName and :fourthTaxonName
108
     */
109
    public function storeClassifiesItsProductsAs(...$taxonsNames)
110
    {
111
        foreach ($taxonsNames as $taxonName) {
112
            $this->taxonRepository->add($this->createTaxon($taxonName));
113
        }
114
    }
115
116
    /**
117
     * @Given /^the store has taxonomy named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/
118
     */
119
    public function theStoreHasTaxonomyNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale)
120
    {
121
        $translationMap = [
122
            $firstLocale => $firstName,
123
            $secondLocale => $secondName,
124
        ];
125
126
        $this->taxonRepository->add($this->createTaxonInManyLanguages($translationMap));
127
    }
128
129
    /**
130
     * @Given /^(it|this product) (belongs to "[^"]+")$/
131
     */
132
    public function itBelongsTo(ProductInterface $product, TaxonInterface $taxon)
133
    {
134
        $product->addTaxon($taxon);
135
136
        $this->objectManager->flush($product);
137
    }
138
139
    /**
140
     * @Given /^the ("[^"]+" taxon) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/
141
     */
142
    public function theTaxonHasAnImageWithACode(TaxonInterface $taxon, $imagePath, $imageCode)
143
    {
144
        $filesPath = $this->getParameter('files_path');
145
146
        $taxonImage = $this->taxonImageFactory->createNew();
147
        $taxonImage->setFile(new UploadedFile($filesPath.$imagePath, basename($imagePath)));
148
        $taxonImage->setCode($imageCode);
149
        $this->imageUploader->upload($taxonImage);
150
151
        $taxon->addImage($taxonImage);
152
153
        $this->objectManager->flush($taxon);
154
    }
155
156
    /**
157
     * @param string $name
158
     *
159
     * @return TaxonInterface
160
     */
161
    private function createTaxon($name)
162
    {
163
        /** @var TaxonInterface $taxon */
164
        $taxon = $this->taxonFactory->createNew();
165
        $taxon->setName($name);
166
        $taxon->setCode(StringInflector::nameToCode($name));
167
        $taxon->setSlug($this->taxonSlugGenerator->generate($name));
168
169
        return $taxon;
170
    }
171
172
    /**
173
     * @param array $names
174
     *
175
     * @return TaxonInterface
176
     */
177
    private function createTaxonInManyLanguages(array $names)
178
    {
179
        /** @var TaxonInterface $taxon */
180
        $taxon = $this->taxonFactory->createNew();
181
        $taxon->setCode(StringInflector::nameToCode($names['en_US']));
182
        foreach ($names as $locale => $name) {
183
            /** @var TranslationInterface $taxonTranslation */
184
            $taxonTranslation = $this->taxonTranslationFactory->createNew();
185
            $taxonTranslation->setLocale($locale);
186
            $taxonTranslation->setName($name);
187
            $taxonTranslation->setSlug($this->taxonSlugGenerator->generate($name));
188
189
            $taxon->addTranslation($taxonTranslation);
190
        }
191
192
        return $taxon;
193
    }
194
195
    /**
196
     * @param string $name
197
     *
198
     * @return NodeElement
199
     */
200
    private function getParameter($name)
201
    {
202
        return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null;
203
    }
204
}
205