Completed
Push — develop ( b7ee57...2e336d )
by
unknown
17:04 queued 09:58
created

ProductVariantCollection::indexRow()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 8.8571
cc 5
eloc 12
nc 9
nop 2
crap 30
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Product;
7
8
use Commercetools\Core\Model\Common\Collection;
9
10
/**
11
 * @package Commercetools\Core\Model\Product
12
 * @method ProductVariant current()
13
 * @method ProductVariantCollection add(ProductVariant $element)
14
 * @method ProductVariant getAt($offset)
15
 */
16
class ProductVariantCollection extends Collection
17
{
18
    const ID = 'id';
19
    const SKU = 'sku';
20
    const MATCHING = 'isMatchingVariant';
21
    protected $type = '\Commercetools\Core\Model\Product\ProductVariant';
22
23
    protected function indexRow($offset, $row)
24
    {
25
        if ($row instanceof ProductVariant) {
26
            $id = $row->getId();
27
            $sku = $row->getSku();
28
            $matching = $row->getIsMatchingVariant();
29
        } else {
30
            $id = isset($row[static::ID])? $row[static::ID] : null;
31
            $sku = isset($row[static::SKU])? $row[static::SKU] : null;
32
            $matching = isset($row[static::MATCHING])? $row[static::MATCHING] : false;
33
        }
34
        $this->addToIndex(static::ID, $offset, $id);
35
        $this->addToIndex(static::SKU, $offset, $sku);
36
        $this->addToIndex(static::MATCHING, $offset, $matching);
37
    }
38
39
    /**
40
     * @param $id
41
     * @return ProductVariant
42
     */
43
    public function getById($id)
44
    {
45
        return $this->getBy(static::ID, $id);
46
    }
47
48
    /**
49
     * @param $id
50
     * @return ProductVariant
51
     */
52
    public function getBySku($id)
53
    {
54
        return $this->getBy(static::SKU, $id);
55
    }
56
57
    public function getMatchingVariant()
58
    {
59
        return $this->getBy(static::MATCHING, true);
60
    }
61
}
62