Completed
Push — develop ( 009567...fb0a26 )
by Nate
08:31
created

IntegrationConnection::getConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 0
cts 10
cp 0
rs 9.8666
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\integration\records;
10
11
use craft\helpers\Json;
12
use flipbox\craft\ember\helpers\ModelHelper;
13
use flipbox\craft\ember\models\HandleRulesTrait;
14
use flipbox\craft\ember\records\ActiveRecordWithId;
15
use flipbox\craft\ember\records\StateAttributeTrait;
16
use yii\validators\UniqueValidator;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.0.0
21
 *
22
 * @property string $class
23
 * @property array $settings
24
 */
25
abstract class IntegrationConnection extends ActiveRecordWithId
26
{
27
    use HandleRulesTrait,
28
        StateAttributeTrait;
29
30
    /**
31
     * @inheritdoc
32
     */
33
    abstract public static function displayName(): string;
34
35
    /**
36
     * @inheritdoc
37
     */
38
    abstract public function getSettingsHtml(): string;
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function init()
44
    {
45
        parent::init();
46
47
        // Normalize settings
48
        $this->setAttribute(
49
            'settings',
50
            static::normalizeSettings($this->getAttribute('settings'))
51
        );
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function rules()
58
    {
59
        return array_merge(
60
            parent::rules(),
61
            $this->handleRules(),
62
            $this->stateRules(),
63
            [
64
                [
65
                    [
66
                        'class',
67
                        'type'
68
                    ],
69
                    'required'
70
                ],
71
                [
72
                    [
73
                        'handle'
74
                    ],
75
                    UniqueValidator::class
76
                ],
77
                [
78
                    [
79
                        'type',
80
                        'class',
81
                        'settings'
82
                    ],
83
                    'safe',
84
                    'on' => [
85
                        ModelHelper::SCENARIO_DEFAULT
86
                    ]
87
                ]
88
            ]
89
        );
90
    }
91
92
    /**
93
     * @param static $record
94
     * @param $row
95
     */
96
    public static function populateRecord($record, $row)
97
    {
98
        parent::populateRecord($record, $row);
99
100
        $settings = static::normalizeSettings($record->settings);
101
102
        $record->setOldAttribute('settings', $settings);
103
        $record->setAttribute('settings', $settings);
104
    }
105
106
    /**
107
     * @param $settings
108
     * @return array
109
     */
110
    protected static function normalizeSettings($settings): array
111
    {
112
        if (is_string($settings)) {
113
            $settings = Json::decodeIfJson($settings);
114
        }
115
116
        if (!is_array($settings)) {
117
            $settings = array_filter([$settings]);
118
        }
119
120
        return $settings;
121
    }
122
}
123