Completed
Push — master ( 2f9340...094334 )
by Kamil
17:24
created

ZoneContext   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 8
Bugs 5 Features 1
Metric Value
wmc 8
c 8
b 5
f 1
lcom 1
cbo 7
dl 0
loc 113
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getZoneByCode() 0 9 2
A getRestOfTheWorldZone() 0 9 2
A thereIsEUZoneContainingAllMembersOfEuropeanUnion() 0 9 1
A thereIsRestOfTheWorldZoneContainingAllOtherCountries() 0 14 1
A defaultTaxZoneIs() 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\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Bundle\AddressingBundle\Factory\ZoneFactoryInterface;
16
use Sylius\Bundle\SettingsBundle\Manager\SettingsManagerInterface;
17
use Sylius\Component\Addressing\Model\ZoneInterface;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
use Symfony\Component\Intl\Intl;
20
21
/**
22
 * @author Mateusz Zalewski <[email protected]>
23
 */
24
final class ZoneContext implements Context
25
{
26
    /**
27
     * @var array
28
     */
29
    private $euMembers = [
30
        'BE', 'BG', 'CZ', 'DK', 'DE', 'EE', 'IE', 'GR', 'ES',
31
        'FR', 'IT', 'CY', 'LV', 'LT', 'LU', 'HU', 'MT', 'NL',
32
        'AT', 'PL', 'PT', 'RO', 'SI', 'SK', 'FI', 'SE', 'GB',
33
    ];
34
35
    /**
36
     * @var RepositoryInterface
37
     */
38
    private $zoneRepository;
39
40
    /**
41
     * @var SettingsManagerInterface
42
     */
43
    private $settingsManager;
44
45
    /**
46
     * @var ZoneFactoryInterface
47
     */
48
    private $zoneFactory;
49
50
    /**
51
     * @param RepositoryInterface $zoneRepository
52
     * @param SettingsManagerInterface $settingsManager
53
     * @param ZoneFactoryInterface $zoneFactory
54
     */
55
    public function __construct(
56
        RepositoryInterface $zoneRepository,
57
        SettingsManagerInterface $settingsManager,
58
        ZoneFactoryInterface $zoneFactory
59
    ) {
60
        $this->zoneRepository = $zoneRepository;
61
        $this->settingsManager = $settingsManager;
62
        $this->zoneFactory = $zoneFactory;
63
    }
64
65
    /**
66
     * @Transform :zone zone
67
     * @Transform zone :zone
68
     * @Transform :zone
69
     */
70
    public function getZoneByCode($zone)
71
    {
72
        $existingZone = $this->zoneRepository->findOneBy(['code' => $zone]);
73
        if (null === $existingZone) {
74
            throw new \InvalidArgumentException(sprintf('Zone with code "%s" does not exist.', $zone));
75
        }
76
77
        return $existingZone;
78
    }
79
80
    /**
81
     * @Transform /^rest of the world$/
82
     * @Transform /^the rest of the world$/
83
     */
84
    public function getRestOfTheWorldZone()
85
    {
86
        $zone = $this->zoneRepository->findOneBy(['code' => 'RoW']);
87
        if (null === $zone) {
88
            throw new \Exception('Rest of the world zone does not exist.');
89
        }
90
91
        return $zone;
92
    }
93
94
    /**
95
     * @Given /^there is "EU" zone containing all members of European Union$/
96
     */
97
    public function thereIsEUZoneContainingAllMembersOfEuropeanUnion()
98
    {
99
        $zone = $this->zoneFactory->createWithMembers($this->euMembers);
100
        $zone->setType(ZoneInterface::TYPE_COUNTRY);
101
        $zone->setCode('EU');
102
        $zone->setName('European Union');
103
104
        $this->zoneRepository->add($zone);
105
    }
106
107
    /**
108
     * @Given /^there is rest of the world zone containing all other countries$/
109
     */
110
    public function thereIsRestOfTheWorldZoneContainingAllOtherCountries()
111
    {
112
        $restOfWorldCountries = array_diff(
113
            array_keys(Intl::getRegionBundle()->getCountryNames('en')),
114
            array_merge($this->euMembers, ['US'])
115
        );
116
117
        $zone = $this->zoneFactory->createWithMembers($restOfWorldCountries);
118
        $zone->setType(ZoneInterface::TYPE_COUNTRY);
119
        $zone->setCode('RoW');
120
        $zone->setName('Rest of the World');
121
122
        $this->zoneRepository->add($zone);
123
    }
124
125
    /**
126
     * @Given default tax zone is :taxZone
127
     */
128
    public function defaultTaxZoneIs($taxZone)
129
    {
130
        $zone = $this->getZoneByCode($taxZone);
131
132
        $settings = $this->settingsManager->loadSettings('sylius_taxation');
133
        $settings->set('default_tax_zone', $zone);
134
        $this->settingsManager->saveSettings('sylius_taxation', $settings);
135
    }
136
}
137