Completed
Push — master ( 18bfc7...82e0b5 )
by Jens
18:00 queued 05:19
created

Delivery   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 29.27 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 12
loc 41
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldDefinitions() 0 8 1
A destinationType() 0 10 2
A fromArray() 12 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 => '\Commercetools\Core\Model\Common\Reference'],
35
        ];
36
    }
37
38
    protected static function destinationType($typeId)
39
    {
40
        $types = [
41
            static::TYPE_MESSAGE => '\Commercetools\Core\Model\Subscription\MessageDelivery',
42
            static::TYPE_RESOURCE_CREATED => '\Commercetools\Core\Model\Subscription\ResourceCreatedDelivery',
43
            static::TYPE_RESOURCE_UPDATED => '\Commercetools\Core\Model\Subscription\ResourceUpdatedDelivery',
44
            static::TYPE_RESOURCE_DELETED => '\Commercetools\Core\Model\Subscription\ResourceDeletedDelivery',
45
        ];
46
        return isset($types[$typeId]) ? $types[$typeId] : '\Commercetools\Core\Model\Subscription\Delivery';
47
    }
48
49 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
        if (get_called_class() == 'Commercetools\Core\Model\Subscription\Delivery' &&
52
            isset($data[static::NOTIFICATION_TYPE])
53
        ) {
54
            $className = static::destinationType($data[static::NOTIFICATION_TYPE]);
55
            if (class_exists($className)) {
56
                return new $className($data, $context);
57
            }
58
        }
59
        return new static($data, $context);
60
    }
61
}
62