Completed
Push — develop ( 0ff0ed...a13565 )
by Nate
12:12
created

IntegrationConnection::findByCondition()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.9666
cc 3
nc 2
nop 1
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;
12
use craft\helpers\Json;
13
use flipbox\craft\ember\helpers\ModelHelper;
14
use flipbox\craft\ember\models\HandleRulesTrait;
15
use flipbox\craft\ember\records\ActiveRecordWithId;
16
use flipbox\craft\integration\connections\ConnectionInterface;
17
use flipbox\craft\integration\queries\IntegrationConnectionQuery;
18
use yii\validators\UniqueValidator;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 2.0.0
23
 *
24
 * @property string $class
25
 * @property array $settings
26
 */
27
abstract class IntegrationConnection extends ActiveRecordWithId implements ConnectionInterface
28
{
29
    use HandleRulesTrait;
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function init()
35
    {
36
        parent::init();
37
38
        // Always this class
39
        $this->class = static::class;
40
    }
41
42
    /**
43
     * @noinspection PhpDocMissingThrowsInspection
44
     * @return IntegrationConnectionQuery
45
     */
46
    public static function find(): IntegrationConnectionQuery
47
    {
48
        /** @noinspection PhpIncompatibleReturnTypeInspection */
49
        /** @noinspection PhpUnhandledExceptionInspection */
50
        return Craft::createObject(IntegrationConnectionQuery::class, [get_called_class()]);
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function rules()
57
    {
58
        return array_merge(
59
            parent::rules(),
60
            $this->handleRules(),
61
            [
62
                [
63
                    [
64
                        'class'
65
                    ],
66
                    'required'
67
                ],
68
                [
69
                    [
70
                        'handle'
71
                    ],
72
                    UniqueValidator::class
73
                ],
74
                [
75
                    [
76
                        'class',
77
                        'settings'
78
                    ],
79
                    'safe',
80
                    'on' => [
81
                        ModelHelper::SCENARIO_DEFAULT
82
                    ]
83
                ]
84
            ]
85
        );
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public static function instantiate($row)
92
    {
93
        $class = $row['class'] ?? static::class;
94
        return new $class;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    protected static function findByCondition($condition)
101
    {
102
        if (!is_numeric($condition) && is_string($condition)) {
103
            $condition = ['handle' => $condition];
104
        }
105
106
        /** @noinspection PhpInternalEntityUsedInspection */
107
        return parent::findByCondition($condition);
108
    }
109
110
    /**
111
     * @param bool $insert
112
     * @param array $changedAttributes
113
     */
114
    public function afterSave($insert, $changedAttributes)
115
    {
116
        parent::afterSave($insert, $changedAttributes);
117
        $this->ensureSettings();
118
    }
119
120
    /**
121
     * @param static $record
122
     * @param $row
123
     */
124
    public static function populateRecord($record, $row)
125
    {
126
        parent::populateRecord($record, $row);
127
        $record->ensureSettings();
128
    }
129
130
    /**
131
     *
132
     */
133
    protected function ensureSettings()
134
    {
135
        $settings = $this->settings;
136
137
        if (is_string($settings)) {
138
            $settings = Json::decodeIfJson($settings);
139
        }
140
141
        $this->setOldAttribute('settings', $settings);
142
        $this->setAttribute('settings', $settings);
143
    }
144
}
145