Passed
Push — develop ( ce160c...798982 )
by Barbara
18:04 queued 14s
created

ShippingRateInputDraft   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 30%

Importance

Changes 0
Metric Value
wmc 11
eloc 18
dl 0
loc 56
ccs 6
cts 20
cp 0.3
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A ofType() 0 4 1
A fieldDefinitions() 0 4 1
A __construct() 0 6 3
A fromArray() 0 9 4
A inputType() 0 7 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Cart;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
11
/**
12
 * @package Commercetools\Core\Model\Cart
13
 * @link http://dev.commercetools.com/http-api-projects-carts.html#shippingrateinput
14
 * @method string getType()
15
 * @method ShippingRateInputDraft setType(string $type = null)
16
 */
17
abstract class ShippingRateInputDraft extends JsonObject
18
{
19
    const INPUT_TYPE = '';
20
21
    /**
22
     * @inheritDoc
23
     */
24 4
    public function __construct(array $data = [], $context = null)
25
    {
26 4
        if (static::INPUT_TYPE != '' && !isset($data[static::TYPE])) {
0 ignored issues
show
introduced by
The condition static::INPUT_TYPE != '' is always false.
Loading history...
27
            $data[static::TYPE] = static::INPUT_TYPE;
28
        }
29 4
        parent::__construct($data, $context);
30 4
    }
31
32 2
    public function fieldDefinitions()
33
    {
34
        return [
35 2
            'type' => [static::TYPE => 'string'],
36
        ];
37
    }
38
39
    /**
40
     * @param array $data
41
     * @param Context|callable $context
42
     * @return static
43
     */
44
    public static function fromArray(array $data, $context = null)
45
    {
46
        if (get_called_class() == ShippingRateInput::class && isset($data[static::TYPE])) {
47
            $className = static::inputType($data[static::TYPE]);
48
            if (class_exists($className)) {
49
                return new $className($data, $context);
50
            }
51
        }
52
        return new static($data, $context);
53
    }
54
55
    protected static function inputType($type)
56
    {
57
        $types = [
58
            ClassificationShippingRateInput::INPUT_TYPE => ClassificationShippingRateInputDraft::class,
59
            ScoreShippingRateInput::INPUT_TYPE => ScoreShippingRateInputDraft::class,
60
        ];
61
        return isset($types[$type]) ? $types[$type] : ShippingRateInputDraft::class;
62
    }
63
64
    /**
65
     * @param string $type
66
     * @param Context|callable $context
67
     * @return static
68
     */
69
    protected static function ofType($type, $context = null)
70
    {
71
        $input = static::of($context);
72
        return $input->setType($type);
73
    }
74
}
75