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

ProductVariantContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProductVariantByNameAndProduct() 0 14 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\Transform;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
16
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
17
18
/**
19
 * @author Łukasz Chruściel <[email protected]>
20
 */
21
final class ProductVariantContext implements Context
22
{
23
    /**
24
     * @var ProductRepositoryInterface
25
     */
26
    private $productRepository;
27
28
    /**
29
     * @var ProductVariantRepositoryInterface
30
     */
31
    private $productVariantRepository;
32
33
    /**
34
     * @param ProductRepositoryInterface $productRepository
35
     * @param ProductVariantRepositoryInterface $productVariantRepository
36
     */
37
    public function __construct(ProductRepositoryInterface $productRepository, ProductVariantRepositoryInterface $productVariantRepository)
38
    {
39
        $this->productRepository = $productRepository;
40
        $this->productVariantRepository = $productVariantRepository;
41
    }
42
43
    /**
44
     * @Transform /^"([^"]+)" variant of product "([^"]+)"$/
45
     */
46
    public function getProductVariantByNameAndProduct($variantName, $productName)
47
    {
48
        $product = $this->productRepository->findOneByName($productName);
49
        if (null === $product) {
50
            throw new \InvalidArgumentException(sprintf('Product with name "%s" does not exist', $productName));
51
        }
52
53
        $productVariant = $this->productVariantRepository->findOneBy(['presentation' => $variantName, 'object' => $product]);
54
        if (null === $productVariant) {
55
            throw new \InvalidArgumentException(sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName));
56
        }
57
58
        return $productVariant;
59
    }
60
}
61