Completed
Push — master ( d0bff1...fc2a6a )
by Dmitry
29:11
created

Bucket::fill()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 3
crap 12
1
<?php
2
3
namespace hiqdev\billing\hiapi\models\relations;
4
5
use hiqdev\php\billing\EntityInterface;
6
use yii\base\InvalidParamException;
7
use yii\helpers\ArrayHelper;
8
9
class Bucket
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $items = [];
15
    /**
16
     * @var string
17
     */
18
    private $sourceKey;
19
20
    /**
21
     * Bucket constructor.
22
     *
23
     * @param string $sourceKey
24
     */
25
    public function __construct($sourceKey)
26
    {
27
        $this->sourceKey = $sourceKey;
28
    }
29
30
    /**
31
     * @param $rows
32
     * @param $sourceKey
33
     * @return static
34
     */
35
    public static function fromRows($rows, $sourceKey)
36
    {
37
        $bucket = new static($sourceKey);
38
        $bucket->initialize($rows);
39
40
        return $bucket;
41
    }
42
43
    protected function initialize($rows)
44
    {
45
        $result = [];
46
        foreach ($rows as $row) {
47
            $key = $row[$this->sourceKey];
48
            $result[$key] = [];
49
        }
50
        $this->items = $result;
51
52
        return $this;
53
    }
54
55
    public function getKeys()
56
    {
57
        return array_keys($this->items);
58
    }
59
60
    /**
61
     * @param EntityInterface[] $entities
62
     * @param string $key the attribute name in $entity that represent this relation
63
     * @param string $entityKey
0 ignored issues
show
Documentation introduced by
There is no parameter named $entityKey. Did you maybe mean $entityIdKey?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
64
     * @return $this
65
     */
66
    public function fill($entities, $key, $entityIdKey)
67
    {
68
        foreach ($entities as $entity) {
69
            $itemKey = ArrayHelper::getValue($entity, $key);
70
            if (!isset($this->items[$itemKey])) {
71
                throw new InvalidParamException('This should not happen. Result was not requested in bucket.');
72
            }
73
            $entityKey = ArrayHelper::getValue($entity, $entityIdKey);
74
            $this->items[$itemKey][$entityKey] = $entity;
75
        }
76
77
        return $this;
78
    }
79
80
    public function pour(&$rows, $relationName)
81
    {
82
        foreach ($rows as &$row) {
83
            $keyName = $this->sourceKey;
84
            $key = $row[$keyName];
85
            $row[$relationName] = $this->items[$key];
86
        }
87
88
        return $this;
89
    }
90
}
91