EnvironmentHelper   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isPermittedEnvironment() 0 14 2
A __construct() 0 1 1
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\Helper;
17
18
use Fp\Collections\ArrayList;
19
use JetBrains\PhpStorm\Immutable;
20
use Symfony\Component\DependencyInjection\Attribute\When;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
23
/** @internal */
24
#[Immutable]
25
final class EnvironmentHelper
26
{
27
    private function __construct() {}
28
29
    public static function isPermittedEnvironment(ContainerBuilder $container, \ReflectionClass $reflectionClass): bool
30
    {
31
        $currentEnvironment = $container->getParameter('kernel.environment');
32
        $permittedEnvironments = ArrayList::collect($reflectionClass->getAttributes(When::class))
33
            ->map(static fn (\ReflectionAttribute $attribute) => $attribute->newInstance()->env)
34
        ;
35
36
        $isEnvironmentIsNotDefined = $permittedEnvironments->isEmpty();
37
        $isEnvironmentPermitted = $permittedEnvironments
38
            ->first(static fn (string $environment) => $environment === $currentEnvironment)
39
            ->isSome()
40
        ;
41
42
        return $isEnvironmentPermitted || $isEnvironmentIsNotDefined;
43
    }
44
}
45