DeployRevision::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DeployRevision;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
8
9
final class DeployRevision
10
{
11
    const SERVICE = 'deploy_revision.worker';
12
13
    /**
14
     * DI container.
15
     *
16
     * @var ContainerBuilder
17
     */
18
    private $container;
19
20 21
    public function __construct()
21
    {
22 21
        $this->container = new ContainerBuilder();
23
24 21
        $loader = new YamlFileLoader($this->container, new FileLocator(dirname(__DIR__)));
25 21
        $loader->load('services.yml');
26 21
    }
27
28
    /**
29
     * @return ContainerBuilder
30
     */
31 4
    public function getContainer()
32
    {
33 4
        return $this->container;
34
    }
35
36
    /**
37
     * @see WorkerInterface::__construct()
38
     *
39
     * @param string $environment
40
     * @param string $versionFile
41
     *
42
     * @return WorkerInterface
43
     */
44 18
    public function getWorker($environment = null, $versionFile = null)
45
    {
46 18
        $parameters = func_get_args();
47
48 18
        foreach (['environment', 'version_file'] as $i => $parameter) {
49 18
            if (isset($parameters[$i])) {
50 7
                $this->container->setParameter("deploy_revision.$parameter", $parameters[$i]);
51 7
            }
52 18
        }
53
54 18
        return $this->getService(static::SERVICE, WorkerInterface::class);
55
    }
56
57
    /**
58
     * Verify a service instance.
59
     *
60
     * @param string $id
61
     *   Service ID.
62
     * @param string $interface
63
     *   FQN of interface which must be implemented by object.
64
     *
65
     * @return mixed
66
     *   An object which implements required interface.
67
     *
68
     * @throws \LogicException
69
     *   When object not implements required interface.
70
     */
71 18
    private function getService($id, $interface)
72
    {
73 18
        $service = $this->container->get($id);
74
75 18
        if ($service instanceof $interface) {
76 17
            return $service;
77
        }
78
79 1
        throw new \LogicException(sprintf('Service "%s" must implement the "%s" interface', $id, $interface));
80
    }
81
}
82