Records   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 154
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A recordClass() 0 4 1
A tableAlias() 0 4 1
A getQuery() 0 4 1
A associationQuery() 0 9 1
A existingAssociations() 0 13 4
A query() 0 13 1
A associations() 0 9 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/meta/license
6
 * @link       https://www.flipboxfactory.com/software/meta/
7
 */
8
9
namespace flipbox\meta\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\meta\db\MetaAssociationsQuery;
16
use flipbox\meta\db\MetaQuery;
17
use flipbox\meta\records\Meta as MetaRecord;
18
use yii\db\ActiveQuery;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @method MetaAssociationsQuery parentGetQuery($config = [])
25
 * @method MetaRecord create(array $attributes = [])
26
 * @method MetaRecord find($identifier)
27
 * @method MetaRecord get($identifier)
28
 * @method MetaRecord findByCondition($condition = [])
29
 * @method MetaRecord getByCondition($condition = [])
30
 * @method MetaRecord findByCriteria($criteria = [])
31
 * @method MetaRecord getByCriteria($criteria = [])
32
 * @method MetaRecord[] findAllByCondition($condition = [])
33
 * @method MetaRecord[] getAllByCondition($condition = [])
34
 * @method MetaRecord[] findAllByCriteria($criteria = [])
35
 * @method MetaRecord[] getAllByCriteria($criteria = [])
36
 */
37
class Records extends SortableAssociations
38
{
39
    use Accessor;
40
41
    /**
42
     * @inheritdoc
43
     */
44
    const SOURCE_ATTRIBUTE = MetaRecord::SOURCE_ATTRIBUTE;
45
46
    /**
47
     * @inheritdoc
48
     */
49
    const TARGET_ATTRIBUTE = MetaRecord::TARGET_ATTRIBUTE;
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public static function recordClass(): string
55
    {
56
        return MetaRecord::class;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    protected static function tableAlias(): string
63
    {
64
        return MetaRecord::tableAlias();
65
    }
66
67
    /**
68
     * @inheritdoc
69
     * @return MetaQuery
70
     */
71
    public function getQuery($config = []): SortableAssociationQueryInterface
72
    {
73
        return new MetaAssociationsQuery(static::recordClass(), $config);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     *
79
     * @param MetaRecord $record
80
     * @return MetaQuery
81
     */
82
    protected function associationQuery(
83
        SortableAssociationInterface $record
84
    ): SortableAssociationQueryInterface {
85
        return $this->query(
86
            $record->{static::SOURCE_ATTRIBUTE},
87
            $record->fieldId,
0 ignored issues
show
Bug introduced by
Accessing fieldId on the interface flipbox\craft\sortable\a...bleAssociationInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
88
            $record->ownerSiteId
0 ignored issues
show
Bug introduced by
Accessing ownerSiteId on the interface flipbox\craft\sortable\a...bleAssociationInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
89
        );
90
    }
91
92
    /**
93
     * @inheritdoc
94
     *
95
     * @param MetaQuery $query
96
     * @return array
97
     */
98
    protected function existingAssociations(
99
        SortableAssociationQueryInterface $query
100
    ): array {
101
        $source = $this->resolveStringAttribute($query, static::SOURCE_ATTRIBUTE);
102
        $field = $this->resolveStringAttribute($query, 'fieldId');
103
        $site = $this->resolveStringAttribute($query, 'siteId');
104
105
        if ($source === null || $field === null || $site === null) {
106
            return [];
107
        }
108
109
        return $this->associations($source, $field, $site);
110
    }
111
112
    /**
113
     * @param $source
114
     * @param int $fieldId
115
     * @param int $siteId
116
     * @return SortableAssociationQueryInterface|ActiveQuery
117
     */
118
    private function query(
119
        $source,
120
        int $fieldId,
121
        int $siteId = null
122
    ): SortableAssociationQueryInterface {
123
        return $this->getQuery()
124
            ->where([
125
                static::SOURCE_ATTRIBUTE => $source,
126
                'fieldId' => $fieldId,
127
                'ownerSiteId' => $siteId
128
            ])
129
            ->orderBy(['sortOrder' => SORT_ASC]);
130
    }
131
132
    /**
133
     * @param $source
134
     * @param int $fieldId
135
     * @param int $siteId
136
     * @return array
137
     */
138
    private function associations(
139
        $source,
140
        int $fieldId,
141
        int $siteId
142
    ): array {
143
        return $this->query($source, $fieldId, $siteId)
0 ignored issues
show
Bug introduced by
The method indexBy does only exist in yii\db\ActiveQuery, but not in flipbox\craft\sortable\a...sociationQueryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
144
            ->indexBy(static::TARGET_ATTRIBUTE)
145
            ->all();
146
    }
147
148
//    /**
149
//     * @param SortableAssociationQueryInterface $query
150
//     * @return \flipbox\domains\fields\Domains|null
151
//     */
152
//    protected function resolveFieldFromQuery(
153
//        SortableAssociationQueryInterface $query
154
//    ) {
155
//        if (null === ($fieldId = $this->resolveStringAttribute($query, 'fieldId'))) {
156
//            return null;
157
//        }
158
//
159
//        return MetaPlugin::getInstance()->getFields()->findById($fieldId);
160
//    }
161
//
162
//    /**
163
//     * @inheritdoc
164
//     * @param bool $validate
165
//     * @throws \Exception
166
//     */
167
//    public function save(
168
//        SortableAssociationQueryInterface $query,
169
//        bool $validate = true
170
//    ): bool {
171
//        if ($validate === true && null !== ($field = $this->resolveFieldFromQuery($query))) {
172
//            $error = '';
173
//
174
//            (new MinMaxValidator([
175
//                'min' => $field->min,
176
//                'max' => $field->max
177
//            ]))->validate($query, $error);
178
//
179
//            if (!empty($error)) {
180
//                MetaPlugin::error(sprintf(
181
//                    "Meta failed to save due to the following validation errors: '%s'",
182
//                    Json::encode($error)
183
//                ));
184
//                return false;
185
//            }
186
//        }
187
//
188
//        return parent::save($query);
189
//    }
190
}
191