Passed
Push — change-design-list-modules-sim... ( 1434af )
by Jesús
04:15
created

AppModuleCreator::fullModuleName()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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
use function strlen;
16
17
final class AppModuleCreator
18
{
19 8
    public function __construct(
20
        private FactoryResolver $factoryResolver,
21
        private ConfigResolver $configResolver,
22
        private DependencyProviderResolver $dependencyProviderResolver,
23
    ) {
24 8
    }
25
26
    /**
27
     * @param class-string $facadeClass
28
     */
29 8
    public function fromClass(string $facadeClass): AppModule
30
    {
31 8
        return new AppModule(
32 8
            $this->fullModuleName($facadeClass),
33 8
            $this->moduleName($facadeClass),
34 8
            $facadeClass,
35 8
            $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...
36 8
            $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...
37 8
            $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...
38 8
        );
39
    }
40
41
    /**
42
     * TODO
43
     *
44
     * @param class-string $facadeClass
45
     */
46 8
    private function fullModuleName(string $facadeClass): string
47
    {
48 8
        $moduleNameIndex = strrpos($facadeClass, '\\') ?: strlen($facadeClass);
49 8
        return substr($facadeClass, 0, $moduleNameIndex);
50
    }
51
52
    /**
53
     * @param class-string $facadeClass
54
     */
55 8
    private function moduleName(string $facadeClass): string
56
    {
57 8
        $parts = explode('\\', $facadeClass);
58 8
        array_pop($parts);
59
60 8
        return (string)end($parts);
61
    }
62
63
    /**
64
     * @param class-string $facadeClass
65
     */
66 8
    private function findFactory(string $facadeClass): ?string
67
    {
68
        try {
69 8
            $resolver = $this->factoryResolver->resolve($facadeClass);
70
71 8
            if ((new ReflectionClass($resolver))->isAnonymous()) {
72 4
                throw new FactoryNotFoundException($facadeClass);
73
            }
74
75 7
            return $resolver::class;
76 4
        } catch (FactoryNotFoundException $e) {
77 4
            return null;
78
        }
79
    }
80
81
    /**
82
     * @param class-string $facadeClass
83
     */
84 8
    private function findConfig(string $facadeClass): ?string
85
    {
86
        try {
87 8
            $resolver = $this->configResolver->resolve($facadeClass);
88
89 8
            if ((new ReflectionClass($resolver))->isAnonymous()) {
90 6
                throw new ConfigNotFoundException($facadeClass);
91
            }
92
93 5
            return $resolver::class;
94 6
        } catch (ConfigNotFoundException $e) {
95 6
            return null;
96
        }
97
    }
98
99
    /**
100
     * @param class-string $facadeClass
101
     */
102 8
    private function findDependencyProvider(string $facadeClass): ?string
103
    {
104
        try {
105 8
            $resolver = $this->dependencyProviderResolver->resolve($facadeClass);
106
107 7
            if ((new ReflectionClass($resolver))->isAnonymous()) {
108
                throw new DependencyProviderNotFoundException($resolver);
109
            }
110 7
            return $resolver::class;
111 4
        } catch (DependencyProviderNotFoundException $e) {
112 4
            return null;
113
        }
114
    }
115
}
116