Completed
Branch develop (4949ff)
by Nate
02:09
created

ActiveRecord   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 50
ccs 0
cts 19
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
recordClass() 0 1 ?
A getDb() 0 7 1
A getQuery() 0 14 1
A prepareQueryConfig() 0 4 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\services\traits\records;
10
11
use flipbox\ember\helpers\QueryHelper;
12
use yii\db\ActiveQuery;
13
use yii\db\ActiveRecord as Record;
14
use yii\db\Connection;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait ActiveRecord
21
{
22
    /**
23
     * @return string
24
     */
25
    public abstract static function recordClass(): string;
26
27
    /**
28
     * @return Connection
29
     */
30
    protected static function getDb(): Connection
31
    {
32
        /** @var Record $recordClass */
33
        $recordClass = static::recordClass();
34
35
        return $recordClass::getDb();
36
    }
37
38
    /*******************************************
39
     * QUERY
40
     *******************************************/
41
42
    /**
43
     * @param array $config
44
     * @return \yii\db\ActiveQuery
45
     */
46
    public function getQuery($config = []): ActiveQuery
47
    {
48
        /** @var Record $recordClass */
49
        $recordClass = static::recordClass();
50
51
        $query = $recordClass::find();
52
53
        QueryHelper::configure(
54
            $query,
55
            $this->prepareQueryConfig($config)
56
        );
57
58
        return $query;
59
    }
60
61
    /**
62
     * @param array $config
63
     * @return array
64
     */
65
    protected function prepareQueryConfig($config = [])
66
    {
67
        return $config;
68
    }
69
}