Passed
Push — master ( 9afe80...f5220c )
by Gabriel
03:51
created

EntrypointLookupFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\Assets\Encore;
4
5
use ByTIC\Config\Config;
0 ignored issues
show
Bug introduced by
The type ByTIC\Config\Config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    public static function create(
28
        string $entrypointJsonPath,
29
        CacheItemPoolInterface $cache = null,
30
        string $cacheKey = null,
31
        bool $strictMode = true
32
    ) {
33
        $entrypointLookup = new EntrypointLookup($entrypointJsonPath, $cache, $cacheKey, $strictMode);
34
35
        return $entrypointLookup;
36
    }
37
38
    /**
39
     * @return array|Config|null
40
     */
41
    public static function getBuilds()
42
    {
43
        if (self::$builds === null) {
44
            self::$builds = self::generateBuilds();
45
        }
46
47
        return self::$builds;
48
    }
49
50
    /**
51
     * @return array|Config
52
     */
53
    protected static function generateBuilds()
54
    {
55
        $config = static::getAssetsConfig();
56
        $buildsConfig = static::getBuildsConfig();
57
        $builds = [
58
            '_default' => static::create($config['output_path'].'/'.static::ENTRYPOINTS_FILE_NAME),
59
        ];
60
        foreach ($buildsConfig as $name => $path) {
61
            $builds[$name] = static::create($path.'/'.static::ENTRYPOINTS_FILE_NAME);
62
        }
63
64
        return $builds;
65
    }
66
67
    /**
68
     * @return array|Config
69
     */
70
    protected static function getBuildsConfig()
71
    {
72
        $config = static::getConfig();
73
        if ($config->has('assets.builds')) {
74
            return $config->get('assets.builds');
75
        }
76
77
        return [];
78
    }
79
80
    /**
81
     * @return array|Config
82
     */
83
    protected static function getAssetsConfig()
84
    {
85
        $config = static::getConfig();
86
        if ($config->has('assets')) {
87
            return $config->get('assets');
88
        }
89
90
        return [];
91
    }
92
93
    /**
94
     * @return Config
95
     */
96
    protected static function getConfig()
97
    {
98
        if (static::$config === null) {
99
            static::setConfig(static::generateConfig());
100
        }
101
102
        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();
0 ignored issues
show
Bug Best Practice introduced by
The expression return config() also could return the type Nip\Config\Config which is incompatible with the documented return type ByTIC\Config\Config.
Loading history...
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
    public static function setConfig($config)
124
    {
125
        if (is_array($config)) {
126
            $config = new Config($config);
127
        }
128
        self::$config = $config;
129
    }
130
131
    public static function reset()
132
    {
133
        static::$config = null;
134
        static::$builds = null;
135
    }
136
}
137