Completed
Push — master ( cec9f3...d27170 )
by Kamil
22:53
created

TaxonomyContext   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 7
c 6
b 1
f 1
lcom 2
cbo 4
dl 0
loc 82
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A storeClassifiesItsProductsAs() 0 10 3
A itBelongsTo() 0 6 1
A createTaxon() 0 8 1
A getCodeFromName() 0 4 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 Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Component\Core\Model\ProductInterface;
17
use Sylius\Component\Core\Model\TaxonInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
21
/**
22
 * @author Mateusz Zalewski <[email protected]>
23
 */
24
final class TaxonomyContext implements Context
25
{
26
    /**
27
     * @var RepositoryInterface
28
     */
29
    private $taxonRepository;
30
31
    /**
32
     * @var FactoryInterface
33
     */
34
    private $taxonFactory;
35
36
    /**
37
     * @var ObjectManager
38
     */
39
    private $objectManager;
40
41
    /**
42
     * @param RepositoryInterface $taxonRepository
43
     * @param FactoryInterface $taxonFactory
44
     * @param ObjectManager $objectManager
45
     */
46
    public function __construct(
47
        RepositoryInterface $taxonRepository,
48
        FactoryInterface $taxonFactory,
49
        ObjectManager $objectManager
50
    ) {
51
        $this->taxonRepository = $taxonRepository;
52
        $this->taxonFactory = $taxonFactory;
53
        $this->objectManager = $objectManager;
54
    }
55
56
    /**
57
     * @Given the store classifies its products as :firstTaxonName
58
     * @Given the store classifies its products as :firstTaxonName and :secondTaxonName
59
     * @Given the store classifies its products as :firstTaxonName, :secondTaxonName and :thirdTaxonName
60
     */
61
    public function storeClassifiesItsProductsAs($firstTaxonName, $secondTaxonName = null, $thirdTaxonName = null)
62
    {
63
        foreach ([$firstTaxonName, $secondTaxonName, $thirdTaxonName] as $taxonName) {
64
            if (null === $taxonName) {
65
                break;
66
            }
67
68
            $this->taxonRepository->add($this->createTaxon($taxonName));
69
        }
70
    }
71
72
    /**
73
     * @Given /^(it) (belongs to "[^"]+")$/
74
     */
75
    public function itBelongsTo(ProductInterface $product, TaxonInterface $taxon)
76
    {
77
        $product->addTaxon($taxon);
78
79
        $this->objectManager->flush($product);
80
    }
81
82
    /**
83
     * @param string $name
84
     *
85
     * @return TaxonInterface
86
     */
87
    private function createTaxon($name)
88
    {
89
        $taxon = $this->taxonFactory->createNew();
90
        $taxon->setName($name);
91
        $taxon->setCode($this->getCodeFromName($name));
92
93
        return $taxon;
94
    }
95
96
    /**
97
     * @param string $name
98
     *
99
     * @return string
100
     */
101
    private function getCodeFromName($name)
102
    {
103
        return str_replace([' ', '-'], '_', strtolower($name));
104
    }
105
}
106