RecordHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 124
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 1
A populate() 0 17 4
A conditionToCriteria() 0 16 3
A configure() 0 12 1
A beginTransaction() 0 4 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\helpers;
10
11
use Craft;
12
use flipbox\spark\records\Record;
13
use yii\base\InvalidConfigException;
14
use yii\db\QueryInterface;
15
use yii\db\Transaction;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class RecordHelper
22
{
23
24
    /**
25
     * The default scenario
26
     */
27
    const DEFAULT_SCENARIO = self::SCENARIO_DEFAULT;
28
29
    /**
30
     * The scenario used by default
31
     */
32
    const SCENARIO_DEFAULT = 'default';
33
34
    /**
35
     * The scenario used to populate a model
36
     */
37
    const SCENARIO_POPULATE = 'populate';
38
39
    /**
40
     * The scenario used to insert a record
41
     */
42
    const SCENARIO_INSERT = 'insert';
43
44
    /**
45
     * The scenario used to update a record
46
     */
47
    const SCENARIO_UPDATE = 'update';
48
49
    /**
50
     * The scenario used to save a record
51
     */
52
    const SCENARIO_SAVE = 'save';
53
54
    /**
55
     * @param $config
56
     * @param string|null $instanceOf
57
     * @param string|null $toScenario
58
     * @return Record
59
     * @throws InvalidConfigException
60
     */
61
    public static function create($config, string $instanceOf = null, string $toScenario = null)
62
    {
63
64
        // Get class from config
65
        $class = ObjectHelper::checkConfig($config, $instanceOf);
66
67
        // New model
68
        $model = new $class();
69
70
        return static::populate($model, $config, $toScenario);
71
    }
72
73
    /**
74
     * @param Record $record
75
     * @param array $properties
76
     * @param string $toScenario
77
     * @return Record
78
     */
79
    public static function populate(Record $record, $properties = [], string $toScenario = null)
80
    {
81
82
        // Set properties
83
        foreach ($properties as $name => $value) {
84
            if ($record->canSetProperty($name)) {
85
                $record->$name = $value;
86
            }
87
        }
88
89
        // Set scenario
90
        if (null !== $toScenario) {
91
            $record->setScenario($toScenario);
92
        }
93
94
        return $record;
95
    }
96
97
    /**
98
     * @param $condition
99
     * @return array
100
     */
101
    public static function conditionToCriteria($condition)
102
    {
103
104
        if (empty($condition)) {
105
            return $condition;
106
        }
107
108
        // Assume it's an id
109
        if (!is_array($condition)) {
110
            $condition = [
111
                'id' => $condition
112
            ];
113
        }
114
115
        return ['where' => ['and', $condition]];
116
    }
117
118
    /**
119
     * @param string|Record $record
120
     * @param $criteria
121
     * @return QueryInterface
122
     */
123
    public static function configure($record, $criteria)
124
    {
125
126
        $query = $record::find();
127
128
        QueryHelper::configure(
129
            $query,
130
            $criteria
131
        );
132
133
        return $query;
134
    }
135
136
137
    /**
138
     * @return Transaction
139
     */
140
    public static function beginTransaction()
141
    {
142
        return Craft::$app->getDb()->beginTransaction();
143
    }
144
}
145