Completed
Push — master ( 749f69...94328c )
by Kamil
27:45
created

ProductContext::castProductNameToProduct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 1
Metric Value
c 2
b 2
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 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 Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Component\Core\Model\ProductInterface;
17
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
21
22
/**
23
 * @author Arkadiusz Krakowiak <[email protected]>
24
 * @author Mateusz Zalewski <[email protected]>
25
 */
26
class ProductContext implements Context
27
{
28
    /**
29
     * @var SharedStorageInterface
30
     */
31
    private $sharedStorage;
32
33
    /**
34
     * @var RepositoryInterface
35
     */
36
    private $productRepository;
37
38
    /**
39
     * @var RepositoryInterface
40
     */
41
    private $taxCategoryRepository;
42
43
    /**
44
     * @var FactoryInterface
45
     */
46
    private $productFactory;
47
48
    /**
49
     * @var ObjectManager
50
     */
51
    private $productManager;
52
53
    /**
54
     * @param SharedStorageInterface $sharedStorage
55
     * @param RepositoryInterface $productRepository
56
     * @param RepositoryInterface $taxCategoryRepository
57
     * @param FactoryInterface $productFactory
58
     * @param ObjectManager $productManager
59
     */
60
    public function __construct(
61
        SharedStorageInterface $sharedStorage,
62
        RepositoryInterface $productRepository,
63
        RepositoryInterface $taxCategoryRepository,
64
        FactoryInterface $productFactory,
65
        ObjectManager $productManager
66
    ) {
67
        $this->sharedStorage = $sharedStorage;
68
        $this->productRepository = $productRepository;
69
        $this->taxCategoryRepository = $taxCategoryRepository;
70
        $this->productFactory = $productFactory;
71
        $this->productManager = $productManager;
72
    }
73
74
    /**
75
     * @Transform /^product "([^"]+)"$/
76
     * @Transform /^"([^"]+)" product$/
77
     */
78
    public function castProductNameToProduct($productName)
79
    {
80
        $product = $this->productRepository->findOneBy(['name' => $productName]);
81
        if (null === $product) {
82
            throw new \InvalidArgumentException('Product with name "'.$productName.'" does not exist');
83
        }
84
85
        return $product;
86
    }
87
88
    /**
89
     * @Given /^store has a product "([^"]+)" priced at "(?:€|£|\$)([^"]+)"$/
90
     */
91
    public function storeHasAProductPricedAt($productName, $price)
92
    {
93
        $product = $this->productFactory->createNew();
94
        $product->setName($productName);
95
        $product->setPrice((int) $price * 100);
96
        $product->setDescription('Awesome '.$productName);
97
98
        $channel = $this->sharedStorage->getCurrentResource('channel');
99
        $product->addChannel($channel);
100
101
        $this->productRepository->add($product);
102
    }
103
104
    /**
105
     * @Given /^(product "[^"]+") belongs to ("[^"]+" tax category)$/
106
     */
107
    public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory)
108
    {
109
        $product->setTaxCategory($taxCategory);
110
        $this->productManager->flush($product);
111
    }
112
}
113