Completed
Push — refactor ( 1ac315 )
by Akihito
05:56
created

InjectorFactory::getInstance()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.5222
cc 5
nc 12
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Compiler;
6
7
use Ray\Compiler\Annotation\Compile;
8
use Ray\Di\AbstractModule;
9
use Ray\Di\Exception\Unbound;
10
use Ray\Di\Injector as RayInjector;
11
use Ray\Di\InjectorInterface;
12
13
use function is_dir;
14
use function mkdir;
15
16
/**
17
 * @psalm-immutable
18
 */
19
final class InjectorFactory
20
{
21
    private function __construct()
22
    {
23
    }
24
25
    /**
26
     * @param callable(): AbstractModule $modules
27
     */
28
    public static function getInstance(callable $modules, string $scriptDir): InjectorInterface
29
    {
30
        ! is_dir($scriptDir) && ! @mkdir($scriptDir) && ! is_dir($scriptDir);
31
        $module = $modules();
32
        $rayInjector = new RayInjector($module, $scriptDir);
33
        $isProd = false;
34
        try {
35
            $isProd = $rayInjector->getInstance('', Compile::class);
36
        } catch (Unbound $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
37
        }
38
39
        return $isProd ? self::getScriptInjector($scriptDir, $module) : $rayInjector;
40
    }
41
42
    private static function getScriptInjector(string $scriptDir, AbstractModule $module): ScriptInjector
43
    {
44
        return new ScriptInjector($scriptDir, static function () use ($scriptDir, $module) {
45
            return new ScriptinjectorModule($scriptDir, $module);
46
        });
47
    }
48
}
49