Completed
Push — master ( b63fa9...5110b4 )
by Kamil
04:39
created

ExternalReference::containerIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace FriendsOfBehat\CrossContainerExtension;
4
5
use Symfony\Component\DependencyInjection\Reference;
6
7
/**
8
 * @internal
9
 */
10
final class ExternalReference
11
{
12
    /**
13
     * @var string
14
     */
15
    private $containerIdentifier;
16
17
    /**
18
     * @var string
19
     */
20
    private $serviceIdentifier;
21
22
    /**
23
     * @param string|Reference $identifierOrReference
24
     *
25
     * @throw \InvalidArgumentException If given argument is not an external reference.
26
     */
27
    public function __construct($identifierOrReference)
28
    {
29
        if ((bool) preg_match('/^__(?P<container_identifier>.+?)__\.(?P<service_identifier>.++)$/', (string) $identifierOrReference, $matches)) {
30
            $this->containerIdentifier = $matches['container_identifier'];
31
            $this->serviceIdentifier = $matches['service_identifier'];
32
        }
33
34
        if (null === $this->containerIdentifier || null === $this->serviceIdentifier) {
35
            throw new \InvalidArgumentException(sprintf(
36
                'Given argument "%s" is not an external reference.',
37
                $identifierOrReference
38
            ));
39
        }
40
    }
41
42
    /**
43
     * @param string|Reference $identifier
44
     *
45
     * @return bool
46
     */
47
    public static function isValid($identifier)
48
    {
49
        try {
50
            new static($identifier);
51
52
            return true;
53
        } catch (\InvalidArgumentException $exception) {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function containerIdentifier()
62
    {
63
        return $this->containerIdentifier;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function serviceIdentifier()
70
    {
71
        return $this->serviceIdentifier;
72
    }
73
}
74