Completed
Push — develop ( fe4302...d9652d )
by Jens
14:25 queued 03:33
created

Delivery::fromArray()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 2
crap 4.0466
1
<?php
2
/**
3
 * @author @jayS-de <[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
11
/**
12
 * @package Commercetools\Core\Model\Subscription
13
 *
14
 * @method string getProjectKey()
15
 * @method Delivery setProjectKey(string $projectKey = null)
16
 * @method string getNotificationType()
17
 * @method Delivery setNotificationType(string $notificationType = null)
18
 * @method Reference getResource()
19
 * @method Delivery setResource(Reference $resource = null)
20
 */
21
class Delivery extends JsonObject
22
{
23
    const NOTIFICATION_TYPE = 'notificationType';
24
    const TYPE_MESSAGE = 'Message';
25
    const TYPE_RESOURCE_CREATED = 'ResourceCreated';
26
    const TYPE_RESOURCE_UPDATED = 'ResourceUpdated';
27
    const TYPE_RESOURCE_DELETED = 'ResourceDeleted';
28
29
    public function fieldDefinitions()
30
    {
31
        return [
32
            'projectKey' => [static::TYPE => 'string'],
33
            static::NOTIFICATION_TYPE => [static::TYPE => 'string'],
34
            'resource' => [static::TYPE => Reference::class],
35
        ];
36
    }
37
38 5
    protected static function destinationType($typeId)
39
    {
40
        $types = [
41 5
            static::TYPE_MESSAGE => MessageDelivery::class,
42 5
            static::TYPE_RESOURCE_CREATED => ResourceCreatedDelivery::class,
43 5
            static::TYPE_RESOURCE_UPDATED => ResourceUpdatedDelivery::class,
44 5
            static::TYPE_RESOURCE_DELETED => ResourceDeletedDelivery::class,
45
        ];
46 5
        return isset($types[$typeId]) ? $types[$typeId] : Delivery::class;
1 ignored issue
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
47
    }
48
49 5 View Code Duplication
    public static function fromArray(array $data, $context = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51 5
        if (get_called_class() == Delivery::class &&
1 ignored issue
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
52 5
            isset($data[static::NOTIFICATION_TYPE])
53
        ) {
54 5
            $className = static::destinationType($data[static::NOTIFICATION_TYPE]);
55 5
            if (class_exists($className)) {
56 5
                return new $className($data, $context);
57
            }
58
        }
59
        return new static($data, $context);
60
    }
61
}
62