Completed
Push — master ( 749f69...94328c )
by Kamil
27:45
created

TaxContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 6
c 3
b 2
f 1
lcom 1
cbo 2
dl 0
loc 104
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A castTaxCategoryNameToTaxCategory() 0 9 2
A storeHasTaxRateWithinZone() 0 18 1
A getAmountFromString() 0 4 1
A getCodeFromName() 0 4 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\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Addressing\Model\ZoneInterface;
16
use Sylius\Component\Resource\Factory\FactoryInterface;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
19
/**
20
 * @author Mateusz Zalewski <[email protected]>
21
 */
22
class TaxContext implements Context
23
{
24
    /**
25
     * @var FactoryInterface
26
     */
27
    private $taxRateFactory;
28
29
    /**
30
     * @var FactoryInterface
31
     */
32
    private $taxCategoryFactory;
33
34
    /**
35
     * @var RepositoryInterface
36
     */
37
    private $taxRateRepository;
38
39
    /**
40
     * @var RepositoryInterface
41
     */
42
    private $taxCategoryRepository;
43
44
    /**
45
     * @var RepositoryInterface
46
     */
47
    private $zoneRepository;
48
49
    /**
50
     * @param FactoryInterface $taxRateFactory
51
     * @param FactoryInterface $taxCategoryFactory
52
     * @param RepositoryInterface $taxRateRepository
53
     * @param RepositoryInterface $taxCategoryRepository
54
     * @param RepositoryInterface $zoneRepository
55
     */
56
    public function __construct(
57
        FactoryInterface $taxRateFactory,
58
        FactoryInterface $taxCategoryFactory,
59
        RepositoryInterface $taxRateRepository,
60
        RepositoryInterface $taxCategoryRepository,
61
        RepositoryInterface $zoneRepository
62
    ) {
63
        $this->taxRateFactory = $taxRateFactory;
64
        $this->taxCategoryFactory = $taxCategoryFactory;
65
        $this->taxRateRepository = $taxRateRepository;
66
        $this->taxCategoryRepository = $taxCategoryRepository;
67
        $this->zoneRepository = $zoneRepository;
68
    }
69
70
    /**
71
     * @Transform /^"([^"]+)" tax category$/
72
     * @Transform /^tax category "([^"]+)"$/
73
     */
74
    public function castTaxCategoryNameToTaxCategory($taxCategoryName)
75
    {
76
        $taxCategory = $this->taxCategoryRepository->findOneBy(['name' => $taxCategoryName]);
77
        if (null === $taxCategory) {
78
            throw new \Exception('Tax category with name "'.$taxCategoryName.'" does not exist');
79
        }
80
81
        return $taxCategory;
82
    }
83
84
    /**
85
     * @Given /^store has "([^"]+)" tax rate of ([^"]+)% for "([^"]+)" within ("([^"]+)" zone)$/
86
     */
87
    public function storeHasTaxRateWithinZone($taxRateName, $taxRateAmount, $taxCategoryName, ZoneInterface $taxZone)
88
    {
89
        $taxCategory = $this->taxCategoryFactory->createNew();
90
        $taxCategory->setName($taxCategoryName);
91
        $taxCategory->setCode($this->getCodeFromName($taxCategoryName));
92
93
        $this->taxCategoryRepository->add($taxCategory);
94
95
        $taxRate = $this->taxRateFactory->createNew();
96
        $taxRate->setName($taxRateName);
97
        $taxRate->setCode($this->getCodeFromName($taxRateName));
98
        $taxRate->setZone($taxZone);
99
        $taxRate->setAmount($this->getAmountFromString($taxRateAmount));
100
        $taxRate->setCategory($taxCategory);
101
        $taxRate->setCalculator('default');
102
103
        $this->taxRateRepository->add($taxRate);
104
    }
105
106
    /**
107
     * @param string $taxRateAmount
108
     *
109
     * @return string
110
     */
111
    private function getAmountFromString($taxRateAmount)
112
    {
113
        return ((int) $taxRateAmount) / 100;
114
    }
115
116
    /**
117
     * @param string $taxRateName
118
     *
119
     * @return string
120
     */
121
    private function getCodeFromName($taxRateName)
122
    {
123
        return str_replace(' ', '_', strtolower($taxRateName));
124
    }
125
}
126