Passed
Push — refactor-FacadeResolver ( 5b5bce )
by Chema
03:48
created

AllAppModulesFinder::createAppModule()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.0071

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 30
ccs 18
cts 19
cp 0.9474
crap 7.0071
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Domain\AllAppModules;
6
7
use Gacela\Framework\AbstractFacade;
8
use OuterIterator;
9
use ReflectionClass;
10
use SplFileInfo;
11
12
final class AllAppModulesFinder
13
{
14 6
    public function __construct(
15
        private OuterIterator $fileIterator,
16
        private AppModuleCreator $appModuleCreator,
17
    ) {
18 6
    }
19
20
    /**
21
     * @return list<AppModule>
22
     */
23 6
    public function findAllAppModules(string $filter): array
24
    {
25 6
        $result = [];
26
27
        /** @var SplFileInfo $fileInfo */
28 6
        foreach ($this->fileIterator as $fileInfo) {
29 6
            $appModule = $this->createAppModule($fileInfo, $filter);
30 6
            if ($appModule !== null && $this->isFacade($appModule)) {
31 6
                $result[$appModule->facadeClass()] = $appModule;
32
            }
33
        }
34 6
        uksort($result, static fn ($a, $b) => $a <=> $b);
35
36 6
        return array_values($result);
37
    }
38
39 6
    private function createAppModule(SplFileInfo $fileInfo, string $filter): ?AppModule
40
    {
41 6
        if (!$fileInfo->isFile()
42 6
            || $fileInfo->getExtension() !== 'php'
43 6
            || str_contains($fileInfo->getRealPath(), 'vendor' . DIRECTORY_SEPARATOR)
44
        ) {
45 4
            return null;
46
        }
47
48 6
        $namespace = $this->getNamespace($fileInfo);
49 6
        $className = $this->buildClassName($fileInfo);
50
51 6
        $fullyQualifiedClassName = sprintf(
52 6
            '%s\\%s',
53 6
            $namespace,
54 6
            $className,
55 6
        );
56
57 6
        if ($filter !== '') {
58 3
            $filterNamespace = str_replace('/', '\\', $filter);
59 3
            if (!str_contains($fullyQualifiedClassName, $filterNamespace)) {
60 3
                return null;
61
            }
62
        }
63
64 6
        if (!class_exists($fullyQualifiedClassName)) {
65
            return null;
66
        }
67
68 6
        return $this->appModuleCreator->fromClass($fullyQualifiedClassName);
69
    }
70
71 6
    private function getNamespace(SplFileInfo $fileInfo): string
72
    {
73 6
        $fileContent = (string)file_get_contents($fileInfo->getRealPath());
74
75 6
        preg_match('#namespace (.*);#', $fileContent, $matches);
76
77 6
        return $matches[1] ?? '';
78
    }
79
80 6
    private function buildClassName(SplFileInfo $fileInfo): string
81
    {
82 6
        $pieces = explode(DIRECTORY_SEPARATOR, $fileInfo->getFilename());
83 6
        $filename = end($pieces);
84
85 6
        return substr($filename, 0, strpos($filename, '.') ?: 1);
86
    }
87
88 6
    private function isFacade(AppModule $appModule): bool
89
    {
90 6
        $rc = new ReflectionClass($appModule->facadeClass());
91 6
        $parentClass = $rc->getParentClass();
92
93 6
        return $parentClass
94 6
            && $parentClass->name === AbstractFacade::class;
95
    }
96
}
97