Completed
Push — develop ( e1fcb3...43936d )
by Nate
04:47
created

Associations::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\domains\services;
10
11
use flipbox\craft\sortable\associations\db\SortableAssociationQueryInterface;
12
use flipbox\craft\sortable\associations\records\SortableAssociationInterface;
13
use flipbox\craft\sortable\associations\services\SortableAssociations;
14
use flipbox\domains\db\DomainsQuery;
15
use flipbox\domains\records\Domain;
16
use yii\db\ActiveQuery;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class Associations extends SortableAssociations
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    const SOURCE_ATTRIBUTE = Domain::SOURCE_ATTRIBUTE;
28
29
    /**
30
     * @inheritdoc
31
     */
32
    const TARGET_ATTRIBUTE = Domain::TARGET_ATTRIBUTE;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected static function tableAlias(): string
38
    {
39
        return Domain::tableAlias();
40
    }
41
42
    /**
43
     * @inheritdoc
44
     * @return DomainsQuery
45
     */
46
    public function getQuery($config = []): SortableAssociationQueryInterface
47
    {
48
        return new DomainsQuery(Domain::class, $config);
49
    }
50
51
    /**
52
     * @param SortableAssociationInterface|Domain $record
53
     * @return SortableAssociationQueryInterface|DomainsQuery
54
     */
55
    protected function associationQuery(
56
        SortableAssociationInterface $record
57
    ): SortableAssociationQueryInterface {
58
        return $this->query(
59
            $record->{static::SOURCE_ATTRIBUTE},
60
            $record->fieldId,
61
            $record->siteId
62
        );
63
    }
64
65
    /**
66
     * @param SortableAssociationQueryInterface|DomainsQuery $query
67
     * @return array
68
     */
69
    protected function existingAssociations(
70
        SortableAssociationQueryInterface $query
71
    ): array {
72
        $source = $this->resolveStringAttribute($query, static::SOURCE_ATTRIBUTE);
73
        $field = $this->resolveStringAttribute($query, 'fieldId');
74
        $site = $this->resolveStringAttribute($query, 'siteId');
75
76
        if ($source === null || $field === null || $site === null) {
77
            return [];
78
        }
79
80
        return $this->associations($source, $field, $site);
81
    }
82
83
    /**
84
     * @param $source
85
     * @param int $fieldId
86
     * @param int $siteId
87
     * @return SortableAssociationQueryInterface|ActiveQuery
88
     */
89
    private function query(
90
        $source,
91
        int $fieldId,
92
        int $siteId
93
    ): SortableAssociationQueryInterface {
94
        return $this->getQuery()
95
            ->where([
96
                static::SOURCE_ATTRIBUTE => $source,
97
                'fieldId' => $fieldId,
98
                'siteId' => $siteId
99
            ])
100
            ->orderBy(['sortOrder' => SORT_ASC]);
101
    }
102
103
    /**
104
     * @param $source
105
     * @param int $fieldId
106
     * @param int $siteId
107
     * @return array
108
     */
109
    private function associations(
110
        $source,
111
        int $fieldId,
112
        int $siteId
113
    ): array {
114
        return $this->query($source, $fieldId, $siteId)
115
            ->indexBy(static::TARGET_ATTRIBUTE)
116
            ->all();
117
    }
118
}
119