Passed
Push — v5 ( 6f63c4...b606a4 )
by Andrew
20:29 queued 15:13
created

SecurityPolicy::getConfigFromFile()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 12
nop 2
dl 0
loc 33
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace nystudio107\crafttwigsandbox\helpers;
4
5
use Craft;
6
use craft\helpers\ArrayHelper;
7
use craft\helpers\StringHelper;
8
use nystudio107\crafttwigsandbox\twig\BaseSecurityPolicy;
9
use nystudio107\seomatic\Seomatic;
0 ignored issues
show
Bug introduced by
The type nystudio107\seomatic\Seomatic 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...
10
use function is_array;
11
12
class SecurityPolicy
13
{
14
    // Static Methods
15
    // =========================================================================
16
17
    public static function createFromFile(string $filePath, ?string $alias = null): BaseSecurityPolicy
18
    {
19
        $config = self::getConfigFromFile($filePath, $alias);
20
21
        return Craft::createObject($config);
22
    }
23
24
    /**
25
     * Loads a config file from, trying @craft/config first, then falling back on
26
     * the provided $alias, if any
27
     *
28
     * @param string $filePath
29
     * @param string|null $alias
30
     *
31
     * @return array
32
     */
33
    public static function getConfigFromFile(string $filePath, ?string $alias = null): array
34
    {
35
        // Try craft/config first
36
        $path = self::getConfigFilePath('@config', $filePath);
37
        if (!file_exists($path)) {
38
            if (!$alias) {
39
                return [];
40
            }
41
            // Now the additional alias config
42
            $path = self::getConfigFilePath($alias, $filePath);
43
            if (!file_exists($path)) {
44
                return [];
45
            }
46
        }
47
48
        if (!is_array($config = @include $path)) {
49
            return [];
50
        }
51
52
        // If it's not a multi-environment config, return the whole thing
53
        if (!array_key_exists('*', $config)) {
54
            return $config;
55
        }
56
57
        $mergedConfig = [];
58
        /** @var array $config */
59
        foreach ($config as $env => $envConfig) {
60
            if ($env === '*' || StringHelper::contains(Seomatic::$environment, $env)) {
61
                $mergedConfig = ArrayHelper::merge($mergedConfig, $envConfig);
62
            }
63
        }
64
65
        return $mergedConfig;
66
    }
67
68
    // Private Methods
69
    // =========================================================================
70
71
    /**
72
     * Return a path from an alias and a partial path
73
     *
74
     * @param string $alias
75
     * @param string $filePath
76
     *
77
     * @return string
78
     */
79
    private static function getConfigFilePath(string $alias, string $filePath): string
80
    {
81
        $path = DIRECTORY_SEPARATOR . ltrim($filePath, DIRECTORY_SEPARATOR);
82
        $path = Craft::getAlias($alias)
0 ignored issues
show
Bug introduced by
Are you sure Craft::getAlias($alias) of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

82
        $path = /** @scrutinizer ignore-type */ Craft::getAlias($alias)
Loading history...
83
            . DIRECTORY_SEPARATOR
84
            . str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path)
85
            . '.php';
86
87
        return $path;
88
    }
89
}
90