Test Failed
Push — develop ( c17639...818a9c )
by Jens
09:56
created

OrderEditResult::fromArray()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 20
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
    public static function fromArray(array $data, $context = null)
34
    {
35
        if (get_called_class() == OrderEditResult::class && isset($data['type'])) {
36
            $className = static::resultType($data['type']);
37
            if (class_exists($className)) {
38
                return new $className($data, $context);
39
            }
40
        }
41
        return new static($data, $context);
42
    }
43
44
    protected static function resultType($typeId)
45
    {
46
        $types = [
47
            OrderEditNotProcessed::ORDER_EDIT_RESULT_TYPE => OrderEditNotProcessed::class,
48
            OrderEditPreviewFailure::ORDER_EDIT_RESULT_TYPE => OrderEditPreviewFailure::class,
49
            OrderEditPreviewSuccess::ORDER_EDIT_RESULT_TYPE => OrderEditPreviewSuccess::class,
50
            OrderEditApplied::ORDER_EDIT_RESULT_TYPE => OrderEditApplied::class,
51
52
        ];
53
        return isset($types[$typeId]) ? $types[$typeId] : OrderEditResult::class;
54
    }
55
}
56