Passed
Branch master (aecce8)
by Brice
05:03
created

ServiceContainer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 77.14%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 119
ccs 27
cts 35
cp 0.7714
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getConfigurationCachePath() 0 8 2
B getInstance() 0 33 6
A __isset() 0 3 1
A __get() 0 3 1
A getConfigurationFilePath() 0 19 3
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
 * This class may be extended to make extra services usable to jobs from workers.
13
 *
14
 * The following services have to be set in /config/services_{env}.yml :
15
 *
16
 * @property \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher
17
 * @property \Psr\Log\LoggerInterface $logger
18
 * @property \JobQueue\Domain\Task\Queue $queue
19
 */
20
class ServiceContainer
21
{
22
    /**
23
     *
24
     * @var self
25
     */
26
    protected static $instance;
27
28
    /**
29
     *
30
     * @var ContainerInterface
31
     */
32
    protected $services;
33
34
    /**
35
     *
36
     * @param ContainerInterface $services
37
     */
38 1
    public function __construct(ContainerInterface $services)
39
    {
40 1
        $this->services = $services;
41 1
    }
42
43
    /**
44
     *
45
     * @return ServiceContainer
46
     */
47 10
    public static function getInstance(): self
48
    {
49 10
        if (self::$instance) {
50 9
            return self::$instance;
51
        }
52
53 1
        $cache = self::getConfigurationCachePath();
54
55 1
        if (Environment::isProd() and is_readable($cache)) {
56
            // Retrieve services from the cache, if exists...
57
            require_once $cache;
58
            $services = new \ProjectServiceContainer;
59
60
        } else {
61
            // ... otherwise compile & cache the configuration
62 1
            $services = new ContainerBuilder;
63
64 1
            $loader = new YamlFileLoader($services, new FileLocator);
65 1
            $loader->load(self::getConfigurationFilePath());
66
67
            // Compile and cache production config
68 1
            if (Environment::isProd()) {
69
                $services->compile(true);
70
71
                if (!is_dir($cacheDir = dirname($cache))) {
72
                    mkdir($cacheDir, 0777, true);
73
                }
74
75
                file_put_contents($cache, (new PhpDumper($services))->dump());
76
            }
77
        }
78
79 1
        return self::$instance = new self($services);
80
    }
81
82
    /**
83
     *
84
     * @return string
85
     */
86 1
    private static function getConfigurationFilePath(): string
87
    {
88
        // Get path from environment variables
89 1
        if ($path = getenv('JOBQUEUE_CONFIG_PATH')) {
90
            return $path;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $path could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
91
        }
92
93
        // Get dir path depending on how component is used
94 1
        if (class_exists('\Composer\Autoload\ClassLoader')) {
95
            // Find config dir using composer ClassLoader class
96 1
            $reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class);
97 1
            $root = dirname(dirname(dirname($reflection->getFileName())));
98
99
        } else {
100
            // If composer has not been user, try to guess the root path
101
            $root = dirname(dirname(__DIR__));
102
        }
103
104 1
        return sprintf('%s/config/services_%s.yml', $root, Environment::getName());
105
    }
106
107
    /**
108
     *
109
     * @return string
110
     */
111 1
    private static function getConfigurationCachePath(): string
112
    {
113
        // Get dir path from environment variables
114 1
        if (!$dir = getenv('JOBQUEUE_CACHE_PATH')) {
115 1
            $dir = sys_get_temp_dir();
116
        }
117
118 1
        return sprintf('%s/jobqueue_services.php', $dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
        return sprintf('%s/jobqueue_services.php', /** @scrutinizer ignore-type */ $dir);
Loading history...
119
    }
120
121
    /**
122
     *
123
     * @param $service
124
     * @return bool
125
     */
126 2
    public function __isset($service)
127
    {
128 2
        return $this->services->has($service);
129
    }
130
131
    /**
132
     *
133
     * @param $service
134
     * @return mixed|object
135
     */
136 10
    public function __get($service)
137
    {
138 10
        return $this->services->get($service);
139
    }
140
}
141