DeployRevision   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 73
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getContainer() 0 4 1
A getWorker() 0 12 3
A getService() 0 10 2
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