Completed
Push — master ( 4b89b9...dd9abc )
by Jens
10:35
created

ApiError   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 3
Bugs 2 Features 3
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 39
ccs 13
cts 14
cp 0.9286
rs 10
c 3
b 2
f 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldDefinitions() 0 7 1
A fromArray() 0 10 3
A pascalcase() 0 12 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Error;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
11
/**
12
 * @package Commercetools\Core\Error
13
 *
14
 * @method string getCode()
15
 * @method ApiError setCode(string $code = null)
16
 * @method string getMessage()
17
 * @method ApiError setMessage(string $message = null)
18
 */
19
class ApiError extends JsonObject
20
{
21 18
    public function fieldDefinitions()
22
    {
23
        return [
24 18
            'code' => [static::TYPE => 'string'],
25 18
            'message' => [static::TYPE => 'string']
26
        ];
27
    }
28
29
    /**
30
     * @param array $data
31
     * @param Context|callable $context
32
     * @return static
33
     */
34 20
    public static function fromArray(array $data, $context = null)
35
    {
36 20
        if (isset($data['code'])) {
37 20
            $className = sprintf('\Commercetools\Core\Error\%sError', self::pascalcase($data['code']));
38 20
            if (class_exists($className)) {
39 20
                return new $className($data, $context);
40
            }
41
        }
42
        return new static($data, $context);
43
    }
44
45 20
    protected static function pascalcase($name)
46
    {
47 20
        return ucfirst(
48
            implode(
49 20
                '',
50
                array_map(
51 20
                    'ucfirst',
52 20
                    explode('_', $name)
53
                )
54
            )
55
        );
56
    }
57
}
58