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

Delivery::fieldDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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