Completed
Push — master ( eeff33...1702a2 )
by Kamil
41:57 queued 20:40
created

ProductContext::getProductByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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\Transform;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Resource\Repository\RepositoryInterface;
16
17
/**
18
 * @author Łukasz Chruściel <[email protected]>
19
 */
20
final class ProductContext implements Context
21
{
22
    /**
23
     * @var RepositoryInterface
24
     */
25
    private $productRepository;
26
27
    /**
28
     * @var RepositoryInterface
29
     */
30
    private $productVariantRepository;
31
32
    /**
33
     * @param RepositoryInterface $productRepository
34
     * @param RepositoryInterface $productVariantRepository
35
     */
36
    public function __construct(RepositoryInterface $productRepository, RepositoryInterface $productVariantRepository)
37
    {
38
        $this->productRepository = $productRepository;
39
        $this->productVariantRepository = $productVariantRepository;
40
    }
41
42
    /**
43
     * @Transform /^product "([^"]+)"$/
44
     * @Transform /^"([^"]+)" product$/
45
     * @Transform :product
46
     */
47
    public function getProductByName($productName)
48
    {
49
        $product = $this->productRepository->findOneBy(['name' => $productName]);
50
        if (null === $product) {
51
            throw new \InvalidArgumentException(sprintf('Product with name "%s" does not exist', $productName));
52
        }
53
54
        return $product;
55
    }
56
57
    /**
58
     * @Transform /^"([^"]+)" variant of product "([^"]+)"$/
59
     */
60
    public function getProductVariantByNameAndProduct($variantName, $productName)
61
    {
62
        $product = $this->getProductByName($productName);
63
64
        $productVariant = $this->productVariantRepository->findOneBy(['presentation' => $variantName, 'object' => $product]);
65
        if (null === $productVariant) {
66
            throw new \InvalidArgumentException(sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName));
67
        }
68
69
        return $productVariant;
70
    }
71
}
72