Passed
Push — improve-list-modules-output ( e63970...70a6e8 )
by Chema
03:02
created

AppModuleCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Domain\AllAppModules;
6
7
use Gacela\Framework\ClassResolver\Config\ConfigNotFoundException;
8
use Gacela\Framework\ClassResolver\Config\ConfigResolver;
9
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderNotFoundException;
10
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderResolver;
11
use Gacela\Framework\ClassResolver\Factory\FactoryNotFoundException;
12
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
13
use ReflectionClass;
14
15
final class AppModuleCreator
16
{
17 7
    public function __construct(
18
        private FactoryResolver $factoryResolver,
19
        private ConfigResolver $configResolver,
20
        private DependencyProviderResolver $dependencyProviderResolver,
21
    ) {
22 7
    }
23
24
    /**
25
     * @param class-string $facadeClass
26
     */
27 7
    public function fromClass(string $facadeClass): AppModule
28
    {
29 7
        return new AppModule(
30 7
            $this->moduleName($facadeClass),
31 7
            $facadeClass,
32 7
            $this->findFactory($facadeClass),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findFactory($facadeClass) targeting Gacela\Console\Domain\Al...eCreator::findFactory() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
33 7
            $this->findConfig($facadeClass),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findConfig($facadeClass) targeting Gacela\Console\Domain\Al...leCreator::findConfig() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34 7
            $this->findDependencyProvider($facadeClass),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findDependencyProvider($facadeClass) targeting Gacela\Console\Domain\Al...indDependencyProvider() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
35 7
        );
36
    }
37
38
    /**
39
     * @param class-string $facadeClass
40
     */
41 7
    private function moduleName(string $facadeClass): string
42
    {
43 7
        $parts = explode('\\', $facadeClass);
44 7
        array_pop($parts);
45
46 7
        return (string)end($parts);
47
    }
48
49
    /**
50
     * @param class-string $facadeClass
51
     */
52 7
    private function findFactory(string $facadeClass): ?string
53
    {
54
        try {
55 7
            $resolver = $this->factoryResolver->resolve($facadeClass);
56
57 7
            if ((new ReflectionClass($resolver))->isAnonymous()) {
58 3
                throw new FactoryNotFoundException($facadeClass);
59
            }
60
61 6
            return $resolver::class;
62 3
        } catch (FactoryNotFoundException $e) {
63 3
            return null;
64
        }
65
    }
66
67
    /**
68
     * @param class-string $facadeClass
69
     */
70 7
    private function findConfig(string $facadeClass): ?string
71
    {
72
        try {
73 7
            $resolver = $this->configResolver->resolve($facadeClass);
74
75 7
            if ((new ReflectionClass($resolver))->isAnonymous()) {
76 5
                throw new ConfigNotFoundException($facadeClass);
77
            }
78
79 4
            return $resolver::class;
80 5
        } catch (ConfigNotFoundException $e) {
81 5
            return null;
82
        }
83
    }
84
85
    /**
86
     * @param class-string $facadeClass
87
     */
88 7
    private function findDependencyProvider(string $facadeClass): ?string
89
    {
90
        try {
91 7
            $resolver = $this->dependencyProviderResolver->resolve($facadeClass);
92
93 6
            if ((new ReflectionClass($resolver))->isAnonymous()) {
94
                throw new DependencyProviderNotFoundException($resolver);
95
            }
96 6
            return $resolver::class;
97 3
        } catch (DependencyProviderNotFoundException $e) {
98 3
            return null;
99
        }
100
    }
101
}
102