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

ProductContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProductByName() 0 9 2
A getProductVariantByNameAndProduct() 0 11 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\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