Passed
Push — develop ( 30dd04...761131 )
by Jens
07:47
created

ShippingRatePriceTier::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 2
crap 3
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\ShippingMethod;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
use Commercetools\Core\Model\Zone\ZoneReference;
11
12
/**
13
 * @package Commercetools\Core\Model\ShippingMethod
14
 * @link https://dev.commercetools.com/http-api-projects-shippingMethods.html#shippingratepricetier
15
 * @method string getType()
16
 * @method ShippingRatePriceTier setType(string $type = null)
17
 */
18
class ShippingRatePriceTier extends JsonObject
19
{
20
    const INPUT_TYPE = '';
21
22
    /**
23
     * @inheritDoc
24
     */
25 8
    public function __construct(array $data = [], $context = null)
26
    {
27 8
        if (static::INPUT_TYPE != '' && !isset($data[static::TYPE])) {
0 ignored issues
show
introduced by
The condition static::INPUT_TYPE != '' && ! IssetNode can never be true.
Loading history...
28 7
            $data[static::TYPE] = static::INPUT_TYPE;
29
        }
30 8
        parent::__construct($data, $context);
31 8
    }
32
33 1
    public function fieldDefinitions()
34
    {
35
        return [
36 1
            'type' => [static::TYPE => 'string'],
37
        ];
38
    }
39
40
    /**
41
     * @param array $data
42
     * @param Context|callable $context
43
     * @return static
44
     */
45 4
    public static function fromArray(array $data, $context = null)
46
    {
47 4
        if (get_called_class() == ShippingRatePriceTier::class && isset($data[static::TYPE])) {
48 4
            $className = static::inputType($data[static::TYPE]);
49 4
            if (class_exists($className)) {
50 4
                return new $className($data, $context);
51
            }
52
        }
53
        return new static($data, $context);
54
    }
55
56 4
    protected static function inputType($type)
57
    {
58
        $types = [
59 4
            CartValue::INPUT_TYPE => CartValue::class,
60 4
            CartClassification::INPUT_TYPE => CartClassification::class,
61 4
            CartScore::INPUT_TYPE => CartScore::class,
62
        ];
63 4
        return isset($types[$type]) ? $types[$type] : ShippingRatePriceTier::class;
64
    }
65
}
66