ApplicationFactory::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Acelaya\Website\Console\Factory;
5
6
use Interop\Container\ContainerInterface;
7
use Interop\Container\Exception\ContainerException;
8
use Symfony\Component\Console\Application as CliApp;
9
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
10
use Zend\ServiceManager\Exception\ServiceNotFoundException;
11
use Zend\ServiceManager\Factory\FactoryInterface;
12
13
class ApplicationFactory implements FactoryInterface
14
{
15
    /**
16
     * Create an object
17
     *
18
     * @param  ContainerInterface $container
19
     * @param  string $requestedName
20
     * @param  null|array $options
21
     * @return object|CliApp
22
     * @throws ServiceNotFoundException if unable to resolve the service.
23
     * @throws ServiceNotCreatedException if an exception is raised when
24
     *     creating a service.
25
     * @throws ContainerException if any other error occurs
26
     */
27
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): CliApp
28
    {
29
        $config = $container->get('config')['cli'];
30
        $commands = $config['commands'] ?? [];
31
32
        $app = new CliApp();
33
        foreach ($commands as $command) {
34
            if (! $container->has($command)) {
35
                continue;
36
            }
37
38
            $app->add($container->get($command));
39
        }
40
41
        return $app;
42
    }
43
}
44