ApplicationFactory::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPFastCGI\FastCGIDaemon;
6
7
use PHPFastCGI\FastCGIDaemon\Command\DaemonRunCommand;
8
use PHPFastCGI\FastCGIDaemon\Driver\DriverContainer;
9
use PHPFastCGI\FastCGIDaemon\Driver\DriverContainerInterface;
10
use Symfony\Component\Console\Application;
11
use Symfony\Component\Console\Command\Command;
12
13
/**
14
 * The default implementation of the ApplicationFactoryInterface.
15
 */
16
final class ApplicationFactory implements ApplicationFactoryInterface
17
{
18
    /**
19
     * @var DriverContainerInterface
20
     */
21
    private $driverContainer;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param DriverContainerInterface $driverContainer The driver container
27
     */
28
    public function __construct(DriverContainerInterface $driverContainer = null)
29
    {
30
        $this->driverContainer = $driverContainer ?: new DriverContainer();
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function createApplication($kernel, string $commandName = null, string $commandDescription = null): Application
37
    {
38
        $command = $this->createCommand($kernel, $commandName, $commandDescription);
39
40
        $application = new Application();
41
        $application->add($command);
42
43
        return $application;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function createCommand($kernel, string $commandName = null, string $commandDescription = null): Command
50
    {
51
        $kernelObject = $this->getKernelObject($kernel);
52
53
        return new DaemonRunCommand($kernelObject, $this->driverContainer, $commandName, $commandDescription);
54
    }
55
56
    /**
57
     * Converts the kernel parameter to an object implementing the KernelInterface
58
     * if it is a callable.
59
     *
60
     * Otherwise returns the object directly.
61
     *
62
     * @param KernelInterface|callable $kernel The kernel
63
     *
64
     * @return KernelInterface The kernel as an object implementing the KernelInterface
65
     */
66
    private function getKernelObject($kernel): KernelInterface
67
    {
68
        if ($kernel instanceof KernelInterface) {
69
            return $kernel;
70
        } elseif (is_callable($kernel)) {
71
            return new CallbackKernel($kernel);
72
        }
73
74
        throw new \InvalidArgumentException('Kernel must be callable or an instance of KernelInterface');
75
    }
76
}
77