Completed
Push — master ( 7c0075...645752 )
by Kamil
18:52
created

TaxonomyContext::itBelongsTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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