Completed
Pull Request — master (#384)
by Kristof
04:13 queued 34s
created

UDB2Projector::applyPlaceImportedFromUDB2()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 59

Duplication

Lines 59
Ratio 100 %

Importance

Changes 0
Metric Value
dl 59
loc 59
rs 8.2723
c 0
b 0
f 0
cc 6
nc 6
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CultuurNet\UDB3\ReadModel\Index;
4
5
use Broadway\EventHandling\EventListenerInterface;
6
use CultuurNet\UDB3\Cdb\ItemBaseAdapterFactory;
7
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
8
use CultuurNet\UDB3\Cdb\ActorItemFactory;
9
use CultuurNet\UDB3\Cdb\EventItemFactory;
10
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
11
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2;
12
use ValueObjects\Web\Domain;
13
14
class UDB2Projector implements EventListenerInterface
15
{
16
    use DelegateEventHandlingToSpecificMethodTrait;
17
18
    /**
19
     * @var RepositoryInterface
20
     */
21
    protected $repository;
22
23
    /**
24
     * @var Domain
25
     */
26
    protected $UDB2Domain;
27
28
    /**
29
     * @var ItemBaseAdapterFactory
30
     */
31
    protected $itemBaseAdapterFactory;
32
33
    /**
34
     * @param RepositoryInterface $repository
35
     * @param ItemBaseAdapterFactory $itemBaseAdapterFactory
36
     * @param Domain $UDB2Domain
37
     */
38
    public function __construct(
39
        RepositoryInterface $repository,
40
        ItemBaseAdapterFactory $itemBaseAdapterFactory,
41
        Domain $UDB2Domain
42
    ) {
43
        $this->repository = $repository;
44
        $this->itemBaseAdapterFactory = $itemBaseAdapterFactory;
45
        $this->UDB2Domain = $UDB2Domain;
46
    }
47
48
    /**
49
     * @param EventImportedFromUDB2 $eventImportedFromUDB2
50
     */
51 View Code Duplication
    protected function applyEventImportedFromUDB2(
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...
52
        EventImportedFromUDB2 $eventImportedFromUDB2
53
    ) {
54
        $itemId = $eventImportedFromUDB2->getEventId();
55
        $udb2Event = EventItemFactory::createEventFromCdbXml(
56
            $eventImportedFromUDB2->getCdbXmlNamespaceUri(),
57
            $eventImportedFromUDB2->getCdbXml()
58
        );
59
60
        $itemType = EntityType::EVENT();
61
62
        $udb2EventAdapter = $this->itemBaseAdapterFactory->create($udb2Event);
63
64
        $userId = $udb2EventAdapter->getResolvedCreatorUserId();
65
66
        /** @var \CultureFeed_Cdb_Data_EventDetail $detail */
67
        $detail = null;
68
        $postalCode = '';
69
        $country = '';
70
71
        $details = $udb2Event->getDetails();
72
        foreach ($details as $languageDetail) {
73
            // The first language detail found will be used.
74
            $detail = $languageDetail;
75
            break;
76
        }
77
78
        $name = trim($detail->getTitle());
79
80
        // Ignore items without a name. They might occur in UDB2 although this
81
        // is not considered normal.
82
        if (empty($name)) {
83
            return;
84
        }
85
86
        $contact_cdb = $udb2Event->getContactInfo();
87
        if ($contact_cdb) {
88
            $addresses = $contact_cdb->getAddresses();
89
90
            /** @var \CultureFeed_Cdb_Data_Address $address */
91
            foreach ($addresses as $address) {
92
                /** @var \CultureFeed_Cdb_Data_Address_PhysicalAddress $physicalAddress */
93
                $physicalAddress = $address->getPhysicalAddress();
94
                if ($physicalAddress) {
95
                    $postalCode = $physicalAddress->getZip();
96
                    $country = $physicalAddress->getCountry();
97
                }
98
            }
99
        }
100
101
        $creationDate = $udb2EventAdapter->getCreationDateTime();
102
103
        $this->repository->updateIndex(
104
            $itemId,
105
            $itemType,
106
            (string) $userId,
107
            $name,
108
            $postalCode,
109
            $country,
110
            $this->UDB2Domain,
111
            $creationDate
112
        );
113
    }
114
115
    /**
116
     * @param PlaceImportedFromUDB2 $placeImportedFromUDB2
117
     */
118 View Code Duplication
    protected function applyPlaceImportedFromUDB2(PlaceImportedFromUDB2 $placeImportedFromUDB2)
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...
119
    {
120
        $placeId = $placeImportedFromUDB2->getActorId();
121
        /** @var \CultureFeed_Cdb_Data_ActorDetail $detail */
122
        $detail = null;
123
        $postalCode = '';
124
        $country = '';
125
126
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
127
            $placeImportedFromUDB2->getCdbXmlNamespaceUri(),
128
            $placeImportedFromUDB2->getCdbXml()
129
        );
130
131
        $udb2ActorAdapter = $this->itemBaseAdapterFactory->create($udb2Actor);
132
133
        $userId = $udb2ActorAdapter->getResolvedCreatorUserId();
134
135
        $details = $udb2Actor->getDetails();
136
        foreach ($details as $languageDetail) {
137
            // The first language detail found will be used.
138
            $detail = $languageDetail;
139
            break;
140
        }
141
142
        $name = trim($detail->getTitle());
143
144
        // Ignore items without a name. They might occur in UDB2 although this
145
        // is not considered normal.
146
        if (empty($name)) {
147
            return;
148
        }
149
150
        $contact_cdb = $udb2Actor->getContactInfo();
151
        if ($contact_cdb) {
152
            $addresses = $contact_cdb->getAddresses();
153
154
            /** @var \CultureFeed_Cdb_Data_Address $address */
155
            foreach ($addresses as $address) {
156
                $physicalAddress = $address->getPhysicalAddress();
157
                if ($physicalAddress) {
158
                    $postalCode = $physicalAddress->getZip();
159
                    $country = $physicalAddress->getCountry();
160
                }
161
            }
162
        }
163
164
        $creationDate = $udb2ActorAdapter->getCreationDateTime();
165
166
        $this->repository->updateIndex(
167
            $placeId,
168
            EntityType::PLACE(),
169
            $userId,
170
            $name,
171
            $postalCode,
172
            $country,
173
            $this->UDB2Domain,
174
            $creationDate
175
        );
176
    }
177
}
178