Destination::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
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Subscription;
7
8
use Commercetools\Core\Model\Common\JsonObject;
9
10
/**
11
 * @package Commercetools\Core\Model\Subscription
12
 *
13
 * @method string getType()
14
 * @method Destination setType(string $type = null)
15
 */
16
class Destination extends JsonObject
17
{
18
    const DESTINATION_SQS = 'SQS';
19
    const DESTINATION_IRON_MQ = 'IronMQ';
20
    const DESTINATION_SNS = 'SNS';
21
    const DESTINATION_AZURE_EVENT_GRID = 'EventGrid';
22
    const DESTINATION_AZURE_SERVICE_BUS = 'AzureServiceBus';
23
    const DESTINATION_GOOGLE_CLOUD_PUB_SUB = 'GoogleCloudPubSub';
24
25 2
    public function fieldDefinitions()
26
    {
27
        return [
28 2
            'type' => [static::TYPE => 'string']
29
        ];
30
    }
31
32
    protected static function destinationType($typeId)
33
    {
34
        $types = [
35
            static::DESTINATION_SQS => SQSDestination::class,
36
            static::DESTINATION_IRON_MQ => IronMQDestination::class,
37
            static::DESTINATION_SNS => SNSDestination::class,
38
            static::DESTINATION_AZURE_EVENT_GRID => AzureEventGridDestination::class,
39
            static::DESTINATION_AZURE_SERVICE_BUS => AzureServiceBusDestination::class,
40
            static::DESTINATION_GOOGLE_CLOUD_PUB_SUB => GoogleCloudPubSubDestination::class,
41
        ];
42
        return isset($types[$typeId]) ? $types[$typeId] : Destination::class;
43
    }
44
45
    public static function fromArray(array $data, $context = null)
46
    {
47
        if (get_called_class() == Destination::class && isset($data[static::TYPE])) {
48
            $className = static::destinationType($data[static::TYPE]);
49
            if (class_exists($className)) {
50
                return new $className($data, $context);
51
            }
52
        }
53
        return new static($data, $context);
54
    }
55
}
56