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

PromotionContext::getCouponByCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
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\Promotion\Repository\PromotionRepositoryInterface;
16
17
/**
18
 * @author Jan Góralski <[email protected]>
19
 */
20
final class PromotionContext implements Context
21
{
22
    /**
23
     * @var PromotionRepositoryInterface
24
     */
25
    private $promotionRepository;
26
27
    /**
28
     * @param PromotionRepositoryInterface $promotionRepository
29
     */
30
    public function __construct(
31
        PromotionRepositoryInterface $promotionRepository
32
    ) {
33
        $this->promotionRepository = $promotionRepository;
34
    }
35
36
    /**
37
     * @Transform /^promotion "([^"]+)"$/
38
     * @Transform /^"([^"]+)" promotion$/
39
     * @Transform :promotion
40
     */
41
    public function getPromotionByName($promotionName)
42
    {
43
        $promotion = $this->promotionRepository->findOneBy(['name' => $promotionName]);
44
        if (null === $promotion) {
45
            throw new \InvalidArgumentException(
46
                sprintf('Promotion with name "%s" does not exist in the promotion repository.', $promotionName)
47
            );
48
        }
49
50
        return $promotion;
51
    }
52
}
53