Passed
Branch php-scrutinizer (0ac9d8)
by Jens
09:19
created

ShippingMethodCollection::indexRow()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.288

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 10
cp 0.8
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 20
nop 2
crap 6.288
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\ShippingMethod;
7
8
use Commercetools\Core\Model\Common\Collection;
9
10
/**
11
 * @package Commercetools\Core\Model\ShippingMethod
12
 * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#shippingmethod
13
 * @method ShippingMethod current()
14
 * @method ShippingMethodCollection add(ShippingMethod $element)
15
 * @method ShippingMethod getAt($offset)
16
 * @method ShippingMethod getById($offset)
17
 */
18
class ShippingMethodCollection extends Collection
19
{
20
    const NAME = 'name';
21
22
    protected $type = ShippingMethod::class;
23
24 5
    protected function indexRow($offset, $row)
25
    {
26 5
        if ($row instanceof ShippingMethod) {
27
            $name = $row->getName();
28
            $id = $row->getId();
29
        } else {
30 5
            $name = isset($row[static::NAME]) ? $row[static::NAME] : null;
31 5
            $id = isset($row[static::ID]) ? $row[static::ID] : null;
32
        }
33 5
        if (!empty($name)) {
34 2
            $this->addToIndex(static::NAME, $offset, $name);
35
        }
36 5
        if (!empty($id)) {
37 2
            $this->addToIndex(static::ID, $offset, $id);
38
        }
39 5
    }
40
41
    /**
42
     * @param $name
43
     * @return ShippingMethod
44
     */
45
    public function getByName($name)
46
    {
47
        return $this->getBy(static::NAME, $name);
48
    }
49
}
50