HttpDestinationAuthentication::fromArray()   A
last analyzed

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
namespace Commercetools\Core\Model\Extension;
6
7
use Commercetools\Core\Model\Common\JsonObject;
8
9
/**
10
 * @package Commercetools\Core\Model\Extension
11
 *
12
 * @method string getType()
13
 * @method HttpDestinationAuthentication setType(string $type = null)
14
 */
15
class HttpDestinationAuthentication extends JsonObject
16
{
17
    const AUTH_TYPE = null;
18
    const AUTH_AUTHORIZATION_HEADER = 'AuthorizationHeader';
19
    const AUTH_AZURE_FUNCTIONS = 'AzureFunctions';
20
21
    /**
22
     * @inheritDoc
23
     */
24 6
    public function __construct(array $data = [], $context = null)
25
    {
26 6
        if (!is_null(static::AUTH_TYPE)) {
0 ignored issues
show
introduced by
The condition is_null(static::AUTH_TYPE) is always true.
Loading history...
27 4
            $data[static::TYPE] = static::AUTH_TYPE;
28
        }
29 6
        parent::__construct($data, $context);
30 6
    }
31
32 2
    public function fieldDefinitions()
33
    {
34
        return [
35 2
            'type' => [static::TYPE => 'string'],
36
        ];
37
    }
38
39
    protected static function authType($typeId)
40
    {
41
        $types = [
42
            static::AUTH_AUTHORIZATION_HEADER => AuthorizationHeaderAuthentication::class,
43
            static::AUTH_AZURE_FUNCTIONS => AzureFunctionsAuthentication::class,
44
        ];
45
        return isset($types[$typeId]) ? $types[$typeId] : Destination::class;
46
    }
47
48
    public static function fromArray(array $data, $context = null)
49
    {
50
        if (get_called_class() == HttpDestinationAuthentication::class && isset($data[static::TYPE])) {
51
            $className = static::authType($data[static::TYPE]);
52
            if (class_exists($className)) {
53
                return new $className($data, $context);
54
            }
55
        }
56
        return new static($data, $context);
57
    }
58
}
59