MetricHelper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 27 2
A resolveSettings() 0 16 4
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/scorecard/license
6
 * @link       https://www.flipboxfactory.com/software/scorecard/
7
 */
8
9
namespace flipbox\craft\scorecard\helpers;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use craft\helpers\Json;
14
use flipbox\craft\ember\helpers\ObjectHelper;
15
use flipbox\craft\scorecard\metrics\MetricInterface;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class MetricHelper
22
{
23
24
    /*******************************************
25
     * CREATE
26
     *******************************************/
27
28
    /**
29
     * @noinspection PhpDocMissingThrowsInspection
30
     *
31
     * @param mixed $config
32
     * @return MetricInterface
33
     */
34
    public static function create($config = []): MetricInterface
35
    {
36
        // Force array
37
        if (!is_array($config)) {
38
            $config = ArrayHelper::toArray($config, [], false);
39
        }
40
41
        // Extract settings
42
        $settings = static::resolveSettings(
43
            ArrayHelper::remove($config, 'settings', [])
44
        );
45
46
        // Merge settings + config
47
        $config = array_merge($config, $settings);
48
49
        /** @noinspection PhpUnhandledExceptionInspection */
50
        $class = ObjectHelper::checkConfig($config, MetricInterface::class);
51
52
        /** @noinspection PhpUnhandledExceptionInspection */
53
        /** @noinspection PhpIncompatibleReturnTypeInspection */
54
        return Craft::createObject(
55
            array_merge(
56
                ['class' => $class],
57
                $config
58
            )
59
        );
60
    }
61
62
    /**
63
     * @param $settings
64
     * @return array
65
     */
66
    public static function resolveSettings($settings): array
67
    {
68
        if (null === $settings) {
69
            return [];
70
        }
71
72
        if (is_string($settings)) {
73
            $settings = Json::decodeIfJson($settings);
74
        }
75
76
        if (!is_array($settings)) {
77
            $settings = ArrayHelper::toArray($settings, [], true);
78
        }
79
80
        return $settings;
81
    }
82
}
83