Passed
Push — master ( 0d508a...6e2a06 )
by Jens
16:22 queued 10s
created

OrderEditResult::fromArray()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
rs 10
cc 4
nc 3
nop 2
crap 4.074
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Commercetools\Core\Model\OrderEdit;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
11
/**
12
 * @package Commercetools\Core\Model\OrderEdit
13
 *
14
 * @method string getType()
15
 * @method OrderEditResult setType(string $type = null)
16
 */
17
class OrderEditResult extends JsonObject
18
{
19
    const ORDER_EDIT_RESULT_TYPE = '';
20
21 4
    public function fieldDefinitions()
22
    {
23
        return [
24 4
            'type' => [static::TYPE => 'string'],
25
        ];
26
    }
27
28
    /**
29
     * @param array $data
30
     * @param Context|callable $context
31
     * @return static
32
     */
33 49
    public static function fromArray(array $data, $context = null)
34
    {
35 49
        if (get_called_class() == OrderEditResult::class && isset($data['type'])) {
36 49
            $className = static::resultType($data['type']);
37 49
            if (class_exists($className)) {
38 49
                return new $className($data, $context);
39
            }
40
        }
41
        return new static($data, $context);
42
    }
43
44 49
    protected static function resultType($typeId)
45
    {
46
        $types = [
47 49
            OrderEditNotProcessed::ORDER_EDIT_RESULT_TYPE => OrderEditNotProcessed::class,
48 49
            OrderEditPreviewFailure::ORDER_EDIT_RESULT_TYPE => OrderEditPreviewFailure::class,
49 49
            OrderEditPreviewSuccess::ORDER_EDIT_RESULT_TYPE => OrderEditPreviewSuccess::class,
50 49
            OrderEditApplied::ORDER_EDIT_RESULT_TYPE => OrderEditApplied::class,
51
52
        ];
53 49
        return isset($types[$typeId]) ? $types[$typeId] : OrderEditResult::class;
54
    }
55
}
56