Services::__isset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CompoLab\Infrastructure;
4
5
use CompoLab\Application\GitlabRepositoryManager;
6
use Gitlab\Client as Gitlab;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
11
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12
13
/**
14
 *
15
 * @property GitlabRepositoryManager $manager
16
 * @property Gitlab $gitlab
17
 */
18
final class Services
19
{
20
    const
21
        CONFIG_DIRS = [__DIR__ . '/../../config'],
22
        CONFIG_NAME = 'services.yml';
23
24
    /** @var self */
25
    private static $instance;
26
27
    /** @var ContainerInterface */
28
    private $services;
29
30
    public function __construct(ContainerInterface $services)
31
    {
32
        $this->services = $services;
33
    }
34
35
    public static function getInstance(bool $cacheEnabled = true): self
36
    {
37
        if (self::$instance) {
38
            return self::$instance;
39
        }
40
41
        $cachePath = sprintf('%s/compolab_services_cache.php', sys_get_temp_dir());
42
43
        if ($cacheEnabled and file_exists($cachePath)) {
44
            // If cache exists, use it...
45
            if (!is_readable($cachePath)) {
46
                throw new \RuntimeException(sprintf('Services cache "%s" is not readable', $cachePath));
47
            }
48
49
            require_once $cachePath;
50
            $services = new \ProjectServiceContainer;
51
52
        } else {
53
            // ... otherwise compile & cache services
54
            $services = new ContainerBuilder;
55
56
            $loader = new YamlFileLoader($services, new FileLocator(self::CONFIG_DIRS));
57
            $loader->load(self::CONFIG_NAME);
58
59
            $services->compile(true);
60
61
            // Compile and cache production config
62
            if ($cacheEnabled) {
63
                if (!is_dir($cacheDir = dirname($cachePath))) {
64
                    mkdir($cacheDir, 0777, true);
65
                }
66
67
                file_put_contents($cachePath, (new PhpDumper($services))->dump());
68
            }
69
        }
70
71
        return self::$instance = new self($services);
72
    }
73
74
    public function getParameter(string $name)
75
    {
76
        return $this->services->getParameter($name);
77
    }
78
79
    /**
80
     *
81
     * @param $service
82
     * @return bool
83
     */
84
    public function __isset($service)
85
    {
86
        return $this->services->has($service);
87
    }
88
89
    /**
90
     *
91
     * @param $service
92
     * @return mixed
93
     * @throws \Exception
94
     */
95
    public function __get($service)
96
    {
97
        return $this->services->get($service);
98
    }
99
}
100