Completed
Push — master ( b70f30...259e2d )
by Kamil
32:06
created

ProductContext::getProductVariantByNameAndProduct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
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\Core\Repository\ProductRepositoryInterface;
16
17
/**
18
 * @author Łukasz Chruściel <[email protected]>
19
 */
20
final class ProductContext implements Context
21
{
22
    /**
23
     * @var ProductRepositoryInterface
24
     */
25
    private $productRepository;
26
    /**
27
     * @param ProductRepositoryInterface $productRepository
28
     */
29
    public function __construct(ProductRepositoryInterface $productRepository)
30
    {
31
        $this->productRepository = $productRepository;
32
    }
33
34
    /**
35
     * @Transform /^product "([^"]+)"$/
36
     * @Transform /^"([^"]+)" product$/
37
     * @Transform :product
38
     */
39
    public function getProductByName($productName)
40
    {
41
        $product = $this->productRepository->findOneByName($productName);
42
        if (null === $product) {
43
            throw new \InvalidArgumentException(sprintf('Product with name "%s" does not exist', $productName));
44
        }
45
46
        return $product;
47
    }
48
}
49