Completed
Push — master ( eeff33...1702a2 )
by Kamil
41:57 queued 20:40
created

ZoneContext::getZoneByCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
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\Resource\Repository\RepositoryInterface;
16
17
/**
18
 * @author Łukasz Chruściel <[email protected]>
19
 */
20
final class ZoneContext implements Context
21
{
22
    /**
23
     * @var RepositoryInterface
24
     */
25
    private $zoneRepository;
26
27
    /**
28
     * @param RepositoryInterface $zoneRepository
29
     */
30
    public function __construct(RepositoryInterface $zoneRepository)
31
    {
32
        $this->zoneRepository = $zoneRepository;
33
    }
34
35
    /**
36
     * @Transform :zone zone
37
     * @Transform zone :zone
38
     * @Transform :zone
39
     */
40
    public function getZoneByCode($zone)
41
    {
42
        $existingZone = $this->zoneRepository->findOneBy(['code' => $zone]);
43
        if (null === $existingZone) {
44
            throw new \InvalidArgumentException(sprintf('Zone with code "%s" does not exist.', $zone));
45
        }
46
47
        return $existingZone;
48
    }
49
50
    /**
51
     * @Transform /^rest of the world$/
52
     * @Transform /^the rest of the world$/
53
     */
54
    public function getRestOfTheWorldZone()
55
    {
56
        $zone = $this->zoneRepository->findOneBy(['code' => 'RoW']);
57
        if (null === $zone) {
58
            throw new \Exception('Rest of the world zone does not exist.');
59
        }
60
61
        return $zone;
62
    }
63
}
64