Failed Conditions
Push — master ( 0ef5da...6c8847 )
by Adrien
11:04 queued 07:56
created

EnumAbstractFactory::canCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Api\Enum;
6
7
use BackedEnum;
1 ignored issue
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Exception;
9
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
10
use Psr\Container\ContainerInterface;
11
12
/**
13
 * Will create GraphQL Enums for a short name or a FQCN of a PHP native backed enum.
14
 *
15
 * The PHP enum must live in `Application\Enum` namespace.
16
 */
17
class EnumAbstractFactory implements AbstractFactoryInterface
18
{
19
    /**
20
     * @var array<class-string<BackedEnum>, PhpEnumType>
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<class-string<BackedEnum>, PhpEnumType> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<BackedEnum>, PhpEnumType>.
Loading history...
21
     */
22
    private array $cache = [];
23
24
    public function canCreate(ContainerInterface $container, $requestedName)
25
    {
26
        $class = $this->getClass($requestedName);
27
28
        return (bool) $class;
29
    }
30
31
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
32
    {
33
        $class = $this->getClass($requestedName);
34
        if (!$class) {
35
            throw new Exception('Cannot create a PhpEnumType for a name not matching a backed enum: ' . $requestedName);
36
        }
37
38
        // Share the same instance between short name and FQCN
39
        if (!array_key_exists($class, $this->cache)) {
40
            $this->cache[$class] = new PhpEnumType($class);
41
        }
42
43
        return $this->cache[$class];
44
    }
45
46
    /**
47
     * @return null|class-string<BackedEnum>
1 ignored issue
show
Documentation Bug introduced by
The doc comment null|class-string<BackedEnum> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in null|class-string<BackedEnum>.
Loading history...
48
     */
49
    private function getClass(string $requestedName): ?string
50
    {
51
        $possibilities = [
52
            $requestedName,
53
            'Application\Enum\\' . $requestedName,
54
        ];
55
56
        foreach ($possibilities as $class) {
57
            if (class_exists($class) && is_a($class, BackedEnum::class, true)) {
58
                return $class;
59
            }
60
        }
61
62
        return null;
63
    }
64
}
65