EventCdbIdExtractor   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 8.08 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 8
loc 99
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A getRelatedPlaceCdbId() 0 13 2
A getRelatedOrganizerCdbId() 0 13 2
B getCdbIdFromEmbeddedLocationOrOrganizer() 8 26 7

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
namespace CultuurNet\UDB3\Cdb\CdbId;
4
5
use CultuurNet\UDB3\Cdb\ExternalId\ArrayMappingService;
6
use CultuurNet\UDB3\Cdb\ExternalId\MappingServiceInterface;
7
8
class EventCdbIdExtractor implements EventCdbIdExtractorInterface
9
{
10
    /**
11
     * @var MappingServiceInterface
12
     */
13
    private $placeExternalIdMappingService;
14
15
    /**
16
     * @var MappingServiceInterface
17
     */
18
    private $organizerExternalIdMappingService;
19
20
    /**
21
     * @param MappingServiceInterface|null $placeExternalIdMappingService
22
     * @param MappingServiceInterface $organizerExternalIdMappingService
23
     */
24
    public function __construct(
25
        MappingServiceInterface $placeExternalIdMappingService = null,
26
        MappingServiceInterface $organizerExternalIdMappingService = null
27
    ) {
28
        if (is_null($placeExternalIdMappingService)) {
29
            $placeExternalIdMappingService = new ArrayMappingService([]);
30
        }
31
        if (is_null($organizerExternalIdMappingService)) {
32
            $organizerExternalIdMappingService = new ArrayMappingService([]);
33
        }
34
35
        $this->placeExternalIdMappingService = $placeExternalIdMappingService;
36
        $this->organizerExternalIdMappingService = $organizerExternalIdMappingService;
37
    }
38
39
    /**
40
     * @param \CultureFeed_Cdb_Item_Event $cdbEvent
41
     * @return string|null
42
     */
43
    public function getRelatedPlaceCdbId(\CultureFeed_Cdb_Item_Event $cdbEvent)
44
    {
45
        $cdbPlace = $cdbEvent->getLocation();
46
47
        if (!is_null($cdbPlace)) {
48
            return $this->getCdbIdFromEmbeddedLocationOrOrganizer(
49
                $cdbPlace,
50
                $this->placeExternalIdMappingService
51
            );
52
        } else {
53
            return null;
54
        }
55
    }
56
57
    /**
58
     * @param \CultureFeed_Cdb_Item_Event $cdbEvent
59
     * @return string|null
60
     */
61
    public function getRelatedOrganizerCdbId(\CultureFeed_Cdb_Item_Event $cdbEvent)
62
    {
63
        $cdbOrganizer = $cdbEvent->getOrganiser();
64
65
        if (!is_null($cdbOrganizer)) {
66
            return $this->getCdbIdFromEmbeddedLocationOrOrganizer(
67
                $cdbOrganizer,
68
                $this->organizerExternalIdMappingService
69
            );
70
        } else {
71
            return null;
72
        }
73
    }
74
75
    /**
76
     * @param \CultureFeed_Cdb_Data_Location|\CultureFeed_Cdb_Data_Organiser $embeddedCdb
77
     * @param MappingServiceInterface $externalIdMappingService
78
     * @return null|string
79
     */
80
    private function getCdbIdFromEmbeddedLocationOrOrganizer(
81
        $embeddedCdb,
82
        MappingServiceInterface $externalIdMappingService
83
    ) {
84
        if (!is_null($embeddedCdb->getCdbid())) {
85
            return $embeddedCdb->getCdbid();
86
        }
87
88
        if (!is_null($embeddedCdb->getExternalId())) {
89
            return $externalIdMappingService->getCdbId(
90
                $embeddedCdb->getExternalId()
91
            );
92
        }
93
94 View Code Duplication
        if (!is_null($embeddedCdb->getActor()) && !is_null($embeddedCdb->getActor()->getCdbId())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
            return $embeddedCdb->getActor()->getCdbId();
96
        }
97
98 View Code Duplication
        if (!is_null($embeddedCdb->getActor()) && !is_null($embeddedCdb->getActor()->getExternalId())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99
            return $externalIdMappingService->getCdbId(
100
                $embeddedCdb->getActor()->getExternalId()
101
            );
102
        }
103
104
        return null;
105
    }
106
}
107