ServiceContainer::getServices()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.1384

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 9
cts 14
cp 0.6429
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 4
nop 0
crap 6.1384
1
<?php
2
3
namespace JobQueue\Infrastructure;
4
5
use Psr\Container\ContainerInterface;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
11
/**
12
 *
13
 * @property \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher
14
 * @property \Psr\Log\LoggerInterface $logger
15
 * @property \JobQueue\Domain\Task\Queue $queue
16
 */
17
final class ServiceContainer
18
{
19
    /**
20
     *
21
     * @var static
22
     */
23
    private static $instance;
24
25
    /**
26
     *
27
     * @var ContainerInterface
28
     */
29
    private $services;
30
31
    /**
32
     *
33
     * @param ContainerInterface $services
34
     */
35 1
    public function __construct(ContainerInterface $services)
36
    {
37 1
        $this->services = $services;
38 1
    }
39
40
    /**
41
     *
42
     * @return static
43
     */
44 5
    public static function getInstance()
45
    {
46 5
        if (self::$instance) {
47 4
            return self::$instance;
48
        }
49
50 1
        $services = self::getServices();
51
52 1
        return self::$instance = new self($services);
53
    }
54
55
    /**
56
     *
57
     * @return ContainerInterface
58
     */
59 1
    private static function getServices(): ContainerInterface
60
    {
61 1
        $cache = sprintf('%s/jobqueue_services.php', sys_get_temp_dir());
62
63 1
        if ('prod' === getenv('JOBQUEUE_ENV') and is_readable($cache)) {
64
            // Retrieve services from the cache, if exists...
65
            require_once $cache;
66
            $services = new \ProjectServiceContainer;
67
68
        } else {
69
            // ... otherwise compile & cache the configuration
70 1
            $services = new ContainerBuilder;
71
72 1
            $loader = new YamlFileLoader($services, new FileLocator);
73 1
            $loader->load(sprintf('%s/config/services.yml', dirname(dirname(__DIR__))));
74
75 1
            $services->compile(true);
76
77
            // Compile and cache production config
78 1
            if ('prod' === getenv('JOBQUEUE_ENV')) {
79
                if (!is_dir($cacheDir = dirname($cache))) {
80
                    mkdir($cacheDir, 0777, true);
81
                }
82
83
                file_put_contents($cache, (new PhpDumper($services))->dump());
84
            }
85
        }
86
87 1
        return $services;
88
    }
89
90
    /**
91
     *
92
     * @param $service
93
     * @return bool
94
     */
95
    public function __isset($service)
96
    {
97
        return $this->services->has($service);
98
    }
99
100
    /**
101
     *
102
     * @param $service
103
     * @return mixed|object
104
     */
105 5
    public function __get($service)
106
    {
107 5
        return $this->services->get($service);
108
    }
109
}
110