Passed
Push — main ( a03838...173f9d )
by Fractal
02:12
created

AsAlias::getServiceAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This is package for Symfony framework.
7
 *
8
 * (c) Mykhailo Shtanko <[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 FRZB\Component\DependencyInjection\Attribute;
15
16
use FRZB\Component\DependencyInjection\Enum\AliasType;
17
use JetBrains\PhpStorm\Immutable;
18
19
#[Immutable]
20
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
21
final class AsAlias
22
{
23
    public readonly AliasType $aliasType;
24
25
    public function __construct(
26
        public readonly string $service,
27
        public readonly bool $isPublic = true,
28
        public readonly ?string $aliasForArgument = null,
29
    ) {
30
        $this->aliasType = match (true) {
0 ignored issues
show
Bug introduced by
The property aliasType is declared read-only in FRZB\Component\Dependenc...ction\Attribute\AsAlias.
Loading history...
31
            !empty($this->aliasForArgument) => AliasType::WithArgumentName,
32
            empty($this->aliasForArgument) => AliasType::WithoutArgumentName,
33
            default => AliasType::LogicException,
34
        };
35
    }
36
37
    public function getServiceAlias(): string
38
    {
39
        return match ($this->aliasType) {
40
            AliasType::WithArgumentName => sprintf('%s %s', $this->service, $this->aliasForArgument),
41
            AliasType::WithoutArgumentName, AliasType::LogicException => $this->service,
42
        };
43
    }
44
}
45