Completed
Push — a-bulwa-travis-is-2 ( 7c64aa )
by Kamil
41:24
created

TaxonomyContext::getCodeFromName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
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 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\Repository\RepositoryInterface;
23
use Symfony\Component\HttpFoundation\File\UploadedFile;
24
25
/**
26
 * @author Mateusz Zalewski <[email protected]>
27
 */
28
final class TaxonomyContext implements Context
29
{
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $taxonRepository;
34
35
    /**
36
     * @var FactoryInterface
37
     */
38
    private $taxonFactory;
39
40
    /**
41
     * @var FactoryInterface
42
     */
43
    private $taxonImageFactory;
44
45
    /**
46
     * @var ObjectManager
47
     */
48
    private $objectManager;
49
50
    /**
51
     * @var ImageUploaderInterface
52
     */
53
    private $imageUploader;
54
55
    /**
56
     * @var array
57
     */
58
    private $minkParameters;
59
60
    /**
61
     * @param RepositoryInterface $taxonRepository
62
     * @param FactoryInterface $taxonFactory
63
     * @param FactoryInterface $taxonImageFactory
64
     * @param ObjectManager $objectManager
65
     * @param ImageUploaderInterface $imageUploader
66
     * @param array $minkParameters
67
     */
68
    public function __construct(
69
        RepositoryInterface $taxonRepository,
70
        FactoryInterface $taxonFactory,
71
        FactoryInterface $taxonImageFactory,
72
        ObjectManager $objectManager,
73
        ImageUploaderInterface $imageUploader,
74
        array $minkParameters
75
    ) {
76
        $this->taxonRepository = $taxonRepository;
77
        $this->taxonFactory = $taxonFactory;
78
        $this->taxonImageFactory = $taxonImageFactory;
79
        $this->objectManager = $objectManager;
80
        $this->imageUploader = $imageUploader;
81
        $this->minkParameters = $minkParameters;
82
    }
83
84
    /**
85
     * @Given the store has :firstTaxonName taxonomy
86
     * @Given the store classifies its products as :firstTaxonName
87
     * @Given the store classifies its products as :firstTaxonName and :secondTaxonName
88
     * @Given the store classifies its products as :firstTaxonName, :secondTaxonName and :thirdTaxonName
89
     * @Given the store classifies its products as :firstTaxonName, :secondTaxonName, :thirdTaxonName and :fourthTaxonName
90
     */
91
    public function storeClassifiesItsProductsAs(...$taxonsNames)
92
    {
93
        foreach ($taxonsNames as $taxonName) {
94
            $this->taxonRepository->add($this->createTaxon($taxonName));
95
        }
96
    }
97
98
    /**
99
     * @Given /^(it|this product) (belongs to "[^"]+")$/
100
     */
101
    public function itBelongsTo(ProductInterface $product, TaxonInterface $taxon)
102
    {
103
        $product->addTaxon($taxon);
104
105
        $this->objectManager->flush($product);
106
    }
107
108
    /**
109
     * @Given /^the ("[^"]+" taxon) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/
110
     */
111
    public function theTaxonHasAnImageWithACode(TaxonInterface $taxon, $imagePath, $imageCode)
112
    {
113
        $filesPath = $this->getParameter('files_path');
114
115
        $taxonImage = $this->taxonImageFactory->createNew();
116
        $taxonImage->setFile(new UploadedFile($filesPath.$imagePath, basename($imagePath)));
117
        $taxonImage->setCode($imageCode);
118
        $this->imageUploader->upload($taxonImage);
119
120
        $taxon->addImage($taxonImage);
121
122
        $this->objectManager->flush($taxon);
123
    }
124
125
    /**
126
     * @param string $name
127
     *
128
     * @return TaxonInterface
129
     */
130
    private function createTaxon($name)
131
    {
132
        /** @var TaxonInterface $taxon */
133
        $taxon = $this->taxonFactory->createNew();
134
        $taxon->setName($name);
135
        $taxon->setCode(StringInflector::nameToCode($name));
136
137
        return $taxon;
138
    }
139
140
    /**
141
     * @param string $name
142
     *
143
     * @return NodeElement
144
     */
145
    private function getParameter($name)
146
    {
147
        return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null;
148
    }
149
}
150