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

ProductTaxonContext::createProductTaxon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
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\ProductTaxonInterface;
18
use Sylius\Component\Core\Model\TaxonInterface;
19
use Sylius\Component\Resource\Factory\FactoryInterface;
20
21
/**
22
 * @author Anna Walasek <[email protected]>
23
 */
24
final class ProductTaxonContext implements Context
25
{
26
    /**
27
     * @var FactoryInterface
28
     */
29
    private $productTaxonFactory;
30
31
    /**
32
     * @var ObjectManager
33
     */
34
    private $objectManager;
35
36
    /**
37
     * @param FactoryInterface $productTaxonFactory
38
     * @param ObjectManager $objectManager
39
     */
40
    public function __construct(
41
        FactoryInterface $productTaxonFactory,
42
        ObjectManager $objectManager
43
    ) {
44
        $this->productTaxonFactory = $productTaxonFactory;
45
        $this->objectManager = $objectManager;
46
    }
47
48
    /**
49
     * @Given /^I assigned (this product) to ("[^"]+" taxon)$/
50
     * @Given /^(it|this product) (belongs to "[^"]+")$/
51
     * @Given /^(this product) is in ("[^"]+" taxon) at (\d)(?:st|nd|rd|th) position$/
52
     */
53
    public function itBelongsTo(ProductInterface $product, TaxonInterface $taxon, $position = null)
54
    {
55
        $productTaxon = $this->createProductTaxon($taxon, $product, (int) $position - 1);
56
        $product->addProductTaxon($productTaxon);
57
58
        $this->objectManager->flush($product);
59
    }
60
    
61
    /**
62
     * @param TaxonInterface $taxon
63
     * @param ProductInterface $product
64
     * @param int|null $position
65
     *
66
     * @return ProductTaxonInterface
67
     */
68
    private function createProductTaxon(TaxonInterface $taxon, ProductInterface $product, $position = null)
69
    {
70
        /** @var ProductTaxonInterface $productTaxon */
71
        $productTaxon = $this->productTaxonFactory->createNew();
72
        $productTaxon->setProduct($product);
73
        $productTaxon->setTaxon($taxon);
74
75
        if(null !== $position) {
76
            $productTaxon->setPosition($position);
77
        }
78
79
        return $productTaxon;
80
    }
81
}
82