Passed
Push — develop ( f8140e...dbb7b9 )
by Jens
10:17
created

Delivery   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 7
eloc 23
dl 0
loc 40
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldDefinitions() 0 7 1
A destinationType() 0 9 2
A fromArray() 0 11 4
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
use Commercetools\Core\Model\Common\Reference;
10
use Commercetools\Core\Model\Message\UserProvidedIdentifiers;
11
12
/**
13
 * @package Commercetools\Core\Model\Subscription
14
 *
15
 * @method string getProjectKey()
16
 * @method Delivery setProjectKey(string $projectKey = null)
17
 * @method string getNotificationType()
18
 * @method Delivery setNotificationType(string $notificationType = null)
19
 * @method Reference getResource()
20
 * @method Delivery setResource(Reference $resource = null)
21
 * @method UserProvidedIdentifiers getResourceUserProvidedIdentifiers()
22
 * @method Delivery setResourceUserProvidedIdentifiers(UserProvidedIdentifiers $resourceUserProvidedIdentifiers = null)
23
 */
24
class Delivery extends JsonObject
25
{
26
    const NOTIFICATION_TYPE = 'notificationType';
27
    const TYPE_MESSAGE = 'Message';
28
    const TYPE_RESOURCE_CREATED = 'ResourceCreated';
29
    const TYPE_RESOURCE_UPDATED = 'ResourceUpdated';
30
    const TYPE_RESOURCE_DELETED = 'ResourceDeleted';
31
32 13
    public function fieldDefinitions()
33
    {
34
        return [
35 13
            'projectKey' => [static::TYPE => 'string'],
36 13
            static::NOTIFICATION_TYPE => [static::TYPE => 'string'],
37 13
            'resource' => [static::TYPE => Reference::class],
38 13
            'resourceUserProvidedIdentifiers' => [static::TYPE => UserProvidedIdentifiers::class],
39
        ];
40
    }
41
42 7
    protected static function destinationType($typeId)
43
    {
44
        $types = [
45 7
            static::TYPE_MESSAGE => MessageDelivery::class,
46 7
            static::TYPE_RESOURCE_CREATED => ResourceCreatedDelivery::class,
47 7
            static::TYPE_RESOURCE_UPDATED => ResourceUpdatedDelivery::class,
48 7
            static::TYPE_RESOURCE_DELETED => ResourceDeletedDelivery::class,
49
        ];
50 7
        return isset($types[$typeId]) ? $types[$typeId] : Delivery::class;
51
    }
52
53 7
    public static function fromArray(array $data, $context = null)
54
    {
55 7
        if (get_called_class() == Delivery::class &&
56 7
            isset($data[static::NOTIFICATION_TYPE])
57
        ) {
58 7
            $className = static::destinationType($data[static::NOTIFICATION_TYPE]);
59 7
            if (class_exists($className)) {
60 7
                return new $className($data, $context);
61
            }
62
        }
63
        return new static($data, $context);
64
    }
65
}
66