Passed
Push — main ( 4569fe...aef34e )
by Chema
01:34
created

ConsoleFactory::getMainContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console;
6
7
use FilesystemIterator;
8
use Gacela\Console\Domain\AllAppModules\AllAppModulesFinder;
9
use Gacela\Console\Domain\AllAppModules\AppModuleCreator;
10
use Gacela\Console\Domain\CommandArguments\CommandArgumentsParser;
11
use Gacela\Console\Domain\CommandArguments\CommandArgumentsParserInterface;
12
use Gacela\Console\Domain\FileContent\FileContentGenerator;
13
use Gacela\Console\Domain\FileContent\FileContentGeneratorInterface;
14
use Gacela\Console\Domain\FileContent\FileContentIoInterface;
15
use Gacela\Console\Domain\FilenameSanitizer\FilenameSanitizer;
16
use Gacela\Console\Domain\FilenameSanitizer\FilenameSanitizerInterface;
17
use Gacela\Console\Infrastructure\FileContentIo;
18
use Gacela\Framework\AbstractFactory;
19
use Gacela\Framework\ClassResolver\Config\ConfigResolver;
20
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
21
use Gacela\Framework\ClassResolver\Provider\ProviderResolver;
22
use Gacela\Framework\Container\Container;
23
use Gacela\Framework\Gacela;
24
use RecursiveDirectoryIterator;
25
use RecursiveIteratorIterator;
26
use Symfony\Component\Console\Command\Command;
27
28
/**
29
 * @extends AbstractFactory<ConsoleConfig>
30
 */
31
final class ConsoleFactory extends AbstractFactory
32
{
33
    /**
34
     * @return list<Command>
35
     *
36 10
     * @psalm-suppress MixedReturnTypeCoercion
37
     */
38 10
    public function getConsoleCommands(): array
39
    {
40
        return (array)$this->getProvidedDependency(ConsoleProvider::COMMANDS);
41 12
    }
42
43 12
    public function createCommandArgumentsParser(): CommandArgumentsParserInterface
44 12
    {
45 12
        return new CommandArgumentsParser(
46
            $this->getConfig()->getComposerJsonContentAsArray(),
47
        );
48 9
    }
49
50 9
    public function createFilenameSanitizer(): FilenameSanitizerInterface
51
    {
52
        return new FilenameSanitizer();
53 10
    }
54
55 10
    public function createFileContentGenerator(): FileContentGeneratorInterface
56 10
    {
57 10
        return new FileContentGenerator(
58 10
            $this->createFileContentIo(),
59
            $this->getTemplateByFilenameMap(),
60
        );
61 6
    }
62
63 6
    public function createAllAppModulesFinder(): AllAppModulesFinder
64 6
    {
65 6
        return new AllAppModulesFinder(
66 6
            $this->createRecursiveIterator(),
67
            $this->createAppModuleCreator(),
68
        );
69 8
    }
70
71 8
    public function createAppModuleCreator(): AppModuleCreator
72 8
    {
73 8
        return new AppModuleCreator(
74 8
            new FactoryResolver(),
75 8
            new ConfigResolver(),
76
            new ProviderResolver(),
77
        );
78 6
    }
79
80 6
    /**
81 6
     * @return array{
82 6
     *     registered_services: int,
83 6
     *     frozen_services: int,
84
     *     factory_services: int,
85
     *     bindings: int,
86 10
     *     cached_dependencies: int,
87
     *     memory_usage: string
88 10
     * }
89
     */
90
    public function getContainerStats(): array
91
    {
92
        return $this->getMainContainer()->getStats();
93
    }
94
95
    /**
96 10
     * @param class-string $className
97
     *
98 10
     * @return list<string>
99
     */
100
    public function getContainerDependencyTree(string $className): array
101
    {
102
        return $this->getMainContainer()->getDependencyTree($className);
103
    }
104
105
    /**
106
     * @return RecursiveIteratorIterator<RecursiveDirectoryIterator>
107
     */
108
    private function createRecursiveIterator(): RecursiveIteratorIterator
109
    {
110
        return new RecursiveIteratorIterator(
111
            new RecursiveDirectoryIterator(Gacela::rootDir(), FilesystemIterator::SKIP_DOTS),
112
            RecursiveIteratorIterator::LEAVES_ONLY,
113
        );
114
    }
115
116
    private function createFileContentIo(): FileContentIoInterface
117
    {
118
        return new FileContentIo();
119
    }
120
121
    /**
122
     * @psalm-suppress MixedReturnTypeCoercion
123
     *
124
     * @return array<string,string>
125
     */
126
    private function getTemplateByFilenameMap(): array
127
    {
128
        return (array)$this->getProvidedDependency(ConsoleProvider::TEMPLATE_BY_FILENAME_MAP);
129
    }
130
131
    private function getMainContainer(): Container
132
    {
133
        return Gacela::container();
134
    }
135
}
136