Completed
Push — remove-content-bundle ( fd7705...1b4274 )
by Kamil
18:36
created

ProductVariantContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 73
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProductVariantByNameAndProduct() 0 17 2
A getProductVariantByName() 0 12 1
A getProductVariantByCode() 0 8 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\Core\Repository\ProductRepositoryInterface;
16
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Łukasz Chruściel <[email protected]>
22
 */
23
final class ProductVariantContext implements Context
24
{
25
    /**
26
     * @var ProductRepositoryInterface
27
     */
28
    private $productRepository;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $productVariantRepository;
34
35
    /**
36
     * @param ProductRepositoryInterface $productRepository
37
     * @param RepositoryInterface $productVariantRepository
38
     */
39
    public function __construct(ProductRepositoryInterface $productRepository, RepositoryInterface $productVariantRepository)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productVariantRepository exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
40
    {
41
        $this->productRepository = $productRepository;
42
        $this->productVariantRepository = $productVariantRepository;
43
    }
44
45
    /**
46
     * @Transform /^"([^"]+)" variant of product "([^"]+)"$/
47
     */
48
    public function getProductVariantByNameAndProduct($variantName, $productName)
49
    {
50
        $products = $this->productRepository->findByName($productName, 'en_US');
51
52
        Assert::eq(
53
            1,
54
            count($products),
55
            sprintf('%d products has been found with name "%s".', count($products), $productName)
56
        );
57
58
        $productVariant = $this->productVariantRepository->findOneBy(['name' => $variantName, 'product' => $products[0]]);
59
        if (null === $productVariant) {
60
            throw new \InvalidArgumentException(sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName));
61
        }
62
63
        return $productVariant;
64
    }
65
66
    /**
67
     * @Transform /^"([^"]+)" product variant$/
68
     * @Transform /^"([^"]+)" variant$/
69
     * @Transform :variant
70
     */
71
    public function getProductVariantByName($name)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
72
    {
73
        $productVariants = $this->productVariantRepository->findByName($name);
0 ignored issues
show
Bug introduced by
The method findByName() does not exist on Sylius\Component\Resourc...ory\RepositoryInterface. Did you maybe mean findBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
74
75
        Assert::eq(
76
            1,
77
            count($productVariants),
78
            sprintf('%d product variants has been found with name "%s".', count($productVariants), $name)
79
        );
80
81
        return $productVariants[0];
82
    }
83
84
    /**
85
     * @Transform /^variant with code "([^"]+)"$/
86
     */
87
    public function getProductVariantByCode($code)
88
    {
89
        $productVariant = $this->productVariantRepository->findOneBy(['code' => $code]);
90
91
        Assert::notNull($productVariant, sprintf('Cannot find product variant with code %s', $code));
92
93
        return $productVariant;
94
    }
95
}
96