Completed
Push — master ( ab6ee6...8da333 )
by Kamil
32:30 queued 09:03
created

ZoneContext::getZone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Transform;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Component\Addressing\Model\ZoneInterface;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
use Webmozart\Assert\Assert;
20
21
final class ZoneContext implements Context
22
{
23
    /** @var RepositoryInterface */
24
    private $zoneRepository;
25
26
    public function __construct(RepositoryInterface $zoneRepository)
27
    {
28
        $this->zoneRepository = $zoneRepository;
29
    }
30
31
    /**
32
     * @Transform /^"([^"]+)" zone$/
33
     * @Transform /^zone "([^"]+)"$/
34
     * @Transform /^zone named "([^"]+)"$/
35
     * @Transform :zone
36
     * @Transform :otherZone
37
     */
38
    public function getZone(string $codeOrName): ZoneInterface
39
    {
40
        $zone = $this->zoneRepository->findOneBy(['code' => $codeOrName]);
41
        if (null !== $zone) {
42
            return $zone;
43
        }
44
45
        $zone = $this->zoneRepository->findOneBy(['name' => $codeOrName]);
46
        Assert::notNull(
47
            $zone,
48
            'Zone does not exist.'
49
        );
50
51
        return $zone;
52
    }
53
54
    /**
55
     * @Transform /^rest of the world$/
56
     */
57
    public function getRestOfTheWorldZone(): ZoneInterface
58
    {
59
        $zone = $this->zoneRepository->findOneBy(['code' => 'RoW']);
60
        Assert::notNull(
61
            $zone,
62
            'Rest of the world zone does not exist.'
63
        );
64
65
        return $zone;
66
    }
67
}
68