ApplicationContainerConfigurator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 62
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ createCommandStorage() 0 21 4
A configureContainer() 0 51 4
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Packages\Application;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Application\Commands\CommandStorage;
22
use Limoncello\Common\Reflection\ClassIsTrait;
23
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as S;
24
use Limoncello\Contracts\Application\CacheSettingsProviderInterface;
25
use Limoncello\Contracts\Application\ContainerConfiguratorInterface;
26
use Limoncello\Contracts\Commands\CommandInterface;
27
use Limoncello\Contracts\Commands\CommandStorageInterface;
28
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
29
use Limoncello\Contracts\Provider\ProvidesCommandsInterface;
30
use Psr\Container\ContainerInterface as PsrContainerInterface;
31
32
/**
33
 * @package Limoncello\Application
34
 */
35
class ApplicationContainerConfigurator implements ContainerConfiguratorInterface
36
{
37
    /** @var callable */
38
    const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME];
39
40
    /**
41 1
     * @inheritdoc
42
     *
43 1
     * @SuppressWarnings(PHPMD.UndefinedVariable)
44
     */
45
    public static function configureContainer(LimoncelloContainerInterface $container): void
46
    {
47
        $container[CommandStorageInterface::class] =
48
            function (PsrContainerInterface $container): CommandStorageInterface {
49
                $creator = new class
50
                {
51
                    use ClassIsTrait;
52
53
                    /**
54
                     * @param string $commandsPath
55 1
                     * @param array  $providerClasses
56
                     *
57
                     * @return CommandStorageInterface
58
                     */
59 1
                    public function createCommandStorage(
60
                        string $commandsPath,
61 1
                        array $providerClasses
62 1
                    ): CommandStorageInterface {
63 1
                        $storage = new CommandStorage();
64
65
                        $interfaceName = CommandInterface::class;
66 1
                        foreach ($this->selectClasses($commandsPath, $interfaceName) as $commandClass) {
67 1
                            $storage->add($commandClass);
68
                        }
69 1
70 1
                        $interfaceName = ProvidesCommandsInterface::class;
71
                        foreach ($this->selectClassImplements($providerClasses, $interfaceName) as $providerClass) {
72
                            /** @var ProvidesCommandsInterface $providerClass */
73
                            foreach ($providerClass::getCommands() as $commandClass) {
74 1
                                $storage->add($commandClass);
75
                            }
76
                        }
77
78
                        return $storage;
79 1
                    }
80 1
                };
81
82 1
                /** @var CacheSettingsProviderInterface $provider */
83 1
                $provider  = $container->get(CacheSettingsProviderInterface::class);
84 1
                $appConfig = $provider->getApplicationConfiguration();
85 1
86
                $providerClasses  = $appConfig[S::KEY_PROVIDER_CLASSES];
87 1
                $commandsFolder   = $appConfig[S::KEY_COMMANDS_FOLDER];
88
                $commandsFileMask = $appConfig[S::KEY_COMMANDS_FILE_MASK] ?? '*.php';
89 1
                $commandsPath     = $commandsFolder . DIRECTORY_SEPARATOR . $commandsFileMask;
90
91
                $storage = $creator->createCommandStorage($commandsPath, $providerClasses);
92
93
                return $storage;
94
            };
95
    }
96
}
97