ExternalReference::serviceIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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