EntrypointLookupFactory   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 78.05%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 122
ccs 32
cts 41
cp 0.7805
rs 10
c 0
b 0
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A generateConfig() 0 10 3
A getAssetsConfig() 0 8 2
A generateBuilds() 0 12 2
A setConfig() 0 6 2
A getBuildsConfig() 0 8 2
A reset() 0 4 1
A getBuilds() 0 7 2
A create() 0 9 1
A getConfig() 0 7 2
1
<?php
2
3
namespace ByTIC\Assets\Encore;
4
5
use Nip\Config\Config;
6
use Psr\Cache\CacheItemPoolInterface;
7
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
8
9
/**
10
 * Class EntrypointLookupFactory
11
 * @package ByTIC\Assets\Encore
12
 */
13
class EntrypointLookupFactory
14
{
15
    protected const ENTRYPOINTS_FILE_NAME = 'entrypoints.json';
16
17
    protected static $config = null;
18
    protected static $builds = null;
19
20
    /**
21
     * @param string $entrypointJsonPath
22
     * @param CacheItemPoolInterface|null $cache
23
     * @param string|null $cacheKey
24
     * @param bool $strictMode
25
     * @return EntrypointLookup
26
     */
27 8
    public static function create(
28
        string $entrypointJsonPath,
29
        CacheItemPoolInterface $cache = null,
30
        string $cacheKey = null,
31
        bool $strictMode = true
32
    ) {
33 8
        $entrypointLookup = new EntrypointLookup($entrypointJsonPath, $cache, $cacheKey, $strictMode);
34
35 8
        return $entrypointLookup;
36
    }
37
38
    /**
39
     * @return array|Config|null
40
     */
41 8
    public static function getBuilds()
42
    {
43 8
        if (self::$builds === null) {
44 8
            self::$builds = self::generateBuilds();
45
        }
46
47 8
        return self::$builds;
48
    }
49
50
    /**
51
     * @return array|Config
52
     */
53 8
    protected static function generateBuilds()
54
    {
55 8
        $config = static::getAssetsConfig();
56 8
        $buildsConfig = static::getBuildsConfig();
57
        $builds = [
58 8
            '_default' => static::create($config['output_path'].'/'.static::ENTRYPOINTS_FILE_NAME),
59
        ];
60 8
        foreach ($buildsConfig as $name => $path) {
61
            $builds[$name] = static::create($path.'/'.static::ENTRYPOINTS_FILE_NAME);
62
        }
63
64 8
        return $builds;
65
    }
66
67
    /**
68
     * @return array|Config
69
     */
70 8
    protected static function getBuildsConfig()
71
    {
72 8
        $config = static::getConfig();
73 8
        if ($config->has('assets.builds')) {
74
            return $config->get('assets.builds');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $config->get('assets.builds') also could return the type string which is incompatible with the documented return type Nip\Config\Config|array.
Loading history...
75
        }
76
77 8
        return [];
78
    }
79
80
    /**
81
     * @return array|Config
82
     */
83 8
    protected static function getAssetsConfig()
84
    {
85 8
        $config = static::getConfig();
86 8
        if ($config->has('assets')) {
87 8
            return $config->get('assets');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $config->get('assets') also could return the type string which is incompatible with the documented return type Nip\Config\Config|array.
Loading history...
88
        }
89
90
        return [];
91
    }
92
93
    /**
94
     * @return Config
95
     */
96 8
    protected static function getConfig()
97
    {
98 8
        if (static::$config === null) {
99
            static::setConfig(static::generateConfig());
100
        }
101
102 8
        return static::$config;
103
    }
104
105
    /**
106
     * @return Config
107
     */
108
    protected static function generateConfig()
109
    {
110
        if (function_exists('config')) {
111
            try {
112
                return \config();
113
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
114
            }
115
        }
116
117
        return new Config();
118
    }
119
120
    /**
121
     * @param Config|array $config
122
     */
123 9
    public static function setConfig($config)
124
    {
125 9
        if (is_array($config)) {
126 9
            $config = new Config($config);
127
        }
128 9
        self::$config = $config;
129 9
    }
130
131 22
    public static function reset()
132
    {
133 22
        static::$config = null;
134 22
        static::$builds = null;
135 22
    }
136
}
137