Completed
Push — master ( 42280b...169348 )
by Nate
10:22 queued 08:33
created

UserAssociations   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 97
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A tableAlias() 0 4 1
A getQuery() 0 4 1
A recordClass() 0 4 1
A associationQuery() 0 7 1
A existingAssociations() 0 11 2
A query() 0 10 1
A associations() 0 8 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\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\ember\services\traits\records\Accessor;
15
use flipbox\organizations\db\UserAssociationQuery;
16
use flipbox\organizations\records\UserAssociation;
17
use yii\db\ActiveQuery;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 *
23
 * @method UserAssociationQuery parentGetQuery($config = [])
24
 * @method UserAssociation create(array $attributes = [])
25
 * @method UserAssociation find($identifier)
26
 * @method UserAssociation get($identifier)
27
 * @method UserAssociation findByCondition($condition = [])
28
 * @method UserAssociation getByCondition($condition = [])
29
 * @method UserAssociation findByCriteria($criteria = [])
30
 * @method UserAssociation getByCriteria($criteria = [])
31
 * @method UserAssociation[] findAllByCondition($condition = [])
32
 * @method UserAssociation[] getAllByCondition($condition = [])
33
 * @method UserAssociation[] findAllByCriteria($criteria = [])
34
 * @method UserAssociation[] getAllByCriteria($criteria = [])
35
 */
36
class UserAssociations extends SortableAssociations
37
{
38
    use Accessor {
39
        getQuery as parentGetQuery;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    const SOURCE_ATTRIBUTE = UserAssociation::SOURCE_ATTRIBUTE;
46
47
    /**
48
     * @return string
49
     */
50
    const TARGET_ATTRIBUTE = UserAssociation::TARGET_ATTRIBUTE;
51
52
    /**
53
     * @inheritdoc
54
     */
55
    protected static function tableAlias(): string
56
    {
57
        return UserAssociation::tableAlias();
58
    }
59
60
    /**
61
     * @param array $config
62
     * @return SortableAssociationQueryInterface|ActiveQuery
63
     */
64
    public function getQuery($config = []): SortableAssociationQueryInterface
65
    {
66
        return $this->parentGetQuery($config);
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public static function recordClass()
73
    {
74
        return UserAssociation::class;
75
    }
76
77
    /**
78
     * @param SortableAssociationInterface|UserAssociation $record
79
     * @return SortableAssociationQueryInterface|UserAssociationQuery
80
     */
81
    protected function associationQuery(
82
        SortableAssociationInterface $record
83
    ): SortableAssociationQueryInterface {
84
        return $this->query(
85
            $record->{static::SOURCE_ATTRIBUTE}
86
        );
87
    }
88
89
    /**
90
     * @param SortableAssociationQueryInterface|UserAssociationQuery $query
91
     * @return array
92
     */
93
    protected function existingAssociations(
94
        SortableAssociationQueryInterface $query
95
    ): array {
96
        $source = $this->resolveStringAttribute($query, static::SOURCE_ATTRIBUTE);
97
98
        if ($source === null) {
99
            return [];
100
        }
101
102
        return $this->associations($source);
103
    }
104
105
    /**
106
     * @param $source
107
     * @return SortableAssociationQueryInterface|UserAssociationQuery
108
     */
109
    private function query(
110
        $source
111
    ): SortableAssociationQueryInterface {
112
        /** @var UserAssociationQuery $query */
113
        $query = $this->getQuery();
114
        return $query->where([
115
            static::SOURCE_ATTRIBUTE => $source
116
        ])
117
            ->orderBy(['sortOrder' => SORT_ASC]);
118
    }
119
120
    /**
121
     * @param $source
122
     * @return array
123
     */
124
    private function associations(
125
        $source
126
    ): array {
127
        /** @var UserAssociationQuery $query */
128
        $query = $this->getQuery($source);
129
        return $query->indexBy(static::TARGET_ATTRIBUTE)
130
            ->all();
131
    }
132
}
133