AsAlias::getServiceAlias()   A
last analyzed

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
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2024 Mykhailo Shtanko [email protected]
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15
16
namespace FRZB\Component\DependencyInjection\Attribute;
17
18
use FRZB\Component\DependencyInjection\Enum\AliasType;
19
use JetBrains\PhpStorm\Immutable;
20
21
#[Immutable]
22
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
23
final class AsAlias
24
{
25
    public readonly AliasType $aliasType;
26
27
    public function __construct(
28
        public readonly string $service,
29
        public readonly bool $isPublic = true,
30
        public readonly ?string $aliasForArgument = null,
31
    ) {
32
        $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...
33
            !empty($this->aliasForArgument) => AliasType::WithArgumentName,
34
            empty($this->aliasForArgument) => AliasType::WithoutArgumentName,
35
            default => AliasType::LogicException,
36
        };
37
    }
38
39
    public function getServiceAlias(): string
40
    {
41
        return match ($this->aliasType) {
42
            AliasType::WithArgumentName => sprintf('%s %s', $this->service, $this->aliasForArgument),
43
            AliasType::WithoutArgumentName, AliasType::LogicException => $this->service,
44
        };
45
    }
46
}
47