Passed
Pull Request — master (#28)
by
unknown
19:15 queued 04:16
created

ExtendedCollection::performOperation()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 4
eloc 10
c 2
b 1
f 1
nc 5
nop 3
dl 0
loc 18
rs 9.9332
1
<?php
2
/**
3
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart;
12
13
use Closure;
14
15
/**
16
 * {@inheritdoc}
17
 */
18
class ExtendedCollection extends Collection
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public $checkConsistency = false;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function collectData($attributes = null)
29
    {
30
        $data = [];
31
        foreach ($this->models as $model) {
32
            if ($this->dataCollector instanceof Closure) {
33
                list($key, $row) = call_user_func($this->dataCollector, $model, $this);
34
            } else {
35
                $key = $model->getPrimaryKey();
36
                $row = $model->getAttributes($this->isConsistent() ? $attributes : $model->attributes);
37
                $row['recordModel'] = $model;
38
            }
39
40
            if ($key) {
41
                $data[$key] = $row;
42
            } else {
43
                $data[] = $row;
44
            }
45
        }
46
47
        return $data;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function performOperation(string $command, array $data, array $queryOptions = []) : array
54
    {
55
        if ($this->isConsistent()) {
56
            return $this->first->batchQuery($command, $data, $queryOptions);
57
        }
58
59
        foreach ($data as $key => $record) {
60
            $records[$record['recordModel']->className()][$key] = $record;
61
        }
62
63
        $results = [];
64
        foreach ($records as $className => $d) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $records seems to be defined by a foreach iteration on line 59. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
65
            $row = reset($d);
0 ignored issues
show
Unused Code introduced by
The assignment to $row is dead and can be removed.
Loading history...
66
            $model = $d['recordModel'];
67
            $results = array_merge($results, $model->batchQuery($command, $d, $queryOptions));
68
        }
69
70
        return $results;
71
    }
72
}
73