Passed
Push — feat/debug-container-command ( cf5949 )
by Chema
11:39 queued 06:29
created

ConsoleFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommandArgumentsParser() 0 4 1
A getTemplateByFilenameMap() 0 3 1
A getContainerStats() 0 3 1
A getConsoleCommands() 0 3 1
A createFileContentGenerator() 0 5 1
A getMainContainer() 0 3 1
A createAppModuleCreator() 0 6 1
A createRecursiveIterator() 0 5 1
A createAllAppModulesFinder() 0 5 1
A createFilenameSanitizer() 0 3 1
A createFileContentIo() 0 3 1
A getContainerDependencyTree() 0 3 1
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\Config\Config;
23
use Gacela\Framework\Container\Container;
24
use Gacela\Framework\Gacela;
25
use RecursiveDirectoryIterator;
26
use RecursiveIteratorIterator;
27
use Symfony\Component\Console\Command\Command;
28
29
/**
30
 * @extends AbstractFactory<ConsoleConfig>
31
 */
32
final class ConsoleFactory extends AbstractFactory
33
{
34
    /**
35
     * @return list<Command>
36
     *
37
     * @psalm-suppress MixedReturnTypeCoercion
38
     */
39
    public function getConsoleCommands(): array
40
    {
41
        return (array)$this->getProvidedDependency(ConsoleProvider::COMMANDS);
42
    }
43
44
    public function createCommandArgumentsParser(): CommandArgumentsParserInterface
45
    {
46
        return new CommandArgumentsParser(
47
            $this->getConfig()->getComposerJsonContentAsArray(),
48
        );
49
    }
50
51
    public function createFilenameSanitizer(): FilenameSanitizerInterface
52
    {
53
        return new FilenameSanitizer();
54
    }
55
56
    public function createFileContentGenerator(): FileContentGeneratorInterface
57
    {
58
        return new FileContentGenerator(
59
            $this->createFileContentIo(),
60
            $this->getTemplateByFilenameMap(),
61
        );
62
    }
63
64
    public function createAllAppModulesFinder(): AllAppModulesFinder
65
    {
66
        return new AllAppModulesFinder(
67
            $this->createRecursiveIterator(),
68
            $this->createAppModuleCreator(),
69
        );
70
    }
71
72
    public function createAppModuleCreator(): AppModuleCreator
73
    {
74
        return new AppModuleCreator(
75
            new FactoryResolver(),
76
            new ConfigResolver(),
77
            new ProviderResolver(),
78
        );
79
    }
80
81
    /**
82
     * @return array{
83
     *     registered_services: int,
84
     *     frozen_services: int,
85
     *     factory_services: int,
86
     *     bindings: int,
87
     *     cached_dependencies: int,
88
     *     memory_usage: string
89
     * }
90
     */
91
    public function getContainerStats(): array
92
    {
93
        return $this->getMainContainer()->getStats();
94
    }
95
96
    /**
97
     * @param class-string $className
98
     *
99
     * @return list<string>
100
     */
101
    public function getContainerDependencyTree(string $className): array
102
    {
103
        return $this->getMainContainer()->getDependencyTree($className);
104
    }
105
106
    /**
107
     * @return RecursiveIteratorIterator<RecursiveDirectoryIterator>
108
     */
109
    private function createRecursiveIterator(): RecursiveIteratorIterator
110
    {
111
        return new RecursiveIteratorIterator(
112
            new RecursiveDirectoryIterator(Gacela::rootDir(), FilesystemIterator::SKIP_DOTS),
113
            RecursiveIteratorIterator::LEAVES_ONLY,
114
        );
115
    }
116
117
    private function createFileContentIo(): FileContentIoInterface
118
    {
119
        return new FileContentIo();
120
    }
121
122
    /**
123
     * @psalm-suppress MixedReturnTypeCoercion
124
     *
125
     * @return array<string,string>
126
     */
127
    private function getTemplateByFilenameMap(): array
128
    {
129
        return (array)$this->getProvidedDependency(ConsoleProvider::TEMPLATE_BY_FILENAME_MAP);
130
    }
131
132
    private function getMainContainer(): Container
133
    {
134
        return Container::withConfig(Config::getInstance());
135
    }
136
}
137