Completed
Push — master ( 1671b1...4e9462 )
by Kamil
23:39
created

PromotionContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getPromotionByName() 0 11 2
A getCouponByCode() 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\Promotion\Repository\PromotionRepositoryInterface;
16
use Sylius\Component\Resource\Repository\RepositoryInterface;
17
18
/**
19
 * @author Jan Góralski <[email protected]>
20
 */
21
final class PromotionContext implements Context
22
{
23
    /**
24
     * @var PromotionRepositoryInterface
25
     */
26
    private $promotionRepository;
27
28
    /**
29
     * @var RepositoryInterface
30
     */
31
    private $couponRepository;
32
33
    /**
34
     * @param PromotionRepositoryInterface $promotionRepository
35
     * @param RepositoryInterface $couponRepository
36
     */
37
    public function __construct(
38
        PromotionRepositoryInterface $promotionRepository,
39
        RepositoryInterface $couponRepository
40
    ) {
41
        $this->promotionRepository = $promotionRepository;
42
        $this->couponRepository = $couponRepository;
43
    }
44
45
    /**
46
     * @Transform /^promotion "([^"]+)"$/
47
     * @Transform /^"([^"]+)" promotion$/
48
     * @Transform :promotion
49
     */
50
    public function getPromotionByName($promotionName)
51
    {
52
        $promotion = $this->promotionRepository->findOneBy(['name' => $promotionName]);
53
        if (null === $promotion) {
54
            throw new \InvalidArgumentException(
55
                sprintf('Promotion with name "%s" does not exist in the promotion repository.', $promotionName)
56
            );
57
        }
58
59
        return $promotion;
60
    }
61
62
    /**
63
     * @Transform /^coupon "([^"]+)"$/
64
     * @Transform /^"([^"]+)" coupon$/
65
     * @Transform :coupon
66
     */
67
    public function getCouponByCode($couponCode)
68
    {
69
        $coupon = $this->couponRepository->findOneBy(['code' => $couponCode]);
70
        if (null === $coupon) {
71
            throw new \InvalidArgumentException(
72
                sprintf('Coupon with code "%s" does not exist in the coupon repository.', $couponCode)
73
            );
74
        }
75
76
        return $coupon;
77
    }
78
}
79