Completed
Pull Request — master (#340)
by Luc
11:03
created

handleEventImportedFromUDB2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Event;
4
5
use CultuurNet\UDB3\Cdb\EventItemFactory;
6
use CultuurNet\UDB3\Event\Commands\UpdateGeoCoordinatesFromAddress;
7
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
8
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
9
use CultuurNet\UDB3\Offer\AbstractGeoCoordinatesProcessManager;
10
11
class GeoCoordinatesProcessManager extends AbstractGeoCoordinatesProcessManager
12
{
13
    /**
14
     * @return array
15
     */
16
    protected function getEventHandlers()
17
    {
18
        return [
19
            EventImportedFromUDB2::class => 'handleEventImportedFromUDB2',
20
            EventUpdatedFromUDB2::class => 'handleEventUpdatedFromUDB2',
21
        ];
22
    }
23
24
    /**
25
     * @param EventImportedFromUDB2 $eventImportedFromUDB2
26
     * @throws \CultureFeed_Cdb_ParseException
27
     */
28
    protected function handleEventImportedFromUDB2(
29
        EventImportedFromUDB2 $eventImportedFromUDB2
30
    ) {
31
        $this->dispatchCommand(
32
            $eventImportedFromUDB2->getCdbXmlNamespaceUri(),
33
            $eventImportedFromUDB2->getCdbXml(),
34
            $eventImportedFromUDB2->getEventId()
35
        );
36
    }
37
38
    /**
39
     * @param EventUpdatedFromUDB2 $eventUpdatedFromUDB2
40
     * @throws \CultureFeed_Cdb_ParseException
41
     */
42
    protected function handleEventUpdatedFromUDB2(
43
        EventUpdatedFromUDB2 $eventUpdatedFromUDB2
44
    ) {
45
        $this->dispatchCommand(
46
            $eventUpdatedFromUDB2->getCdbXmlNamespaceUri(),
47
            $eventUpdatedFromUDB2->getCdbXml(),
48
            $eventUpdatedFromUDB2->getEventId()
49
        );
50
    }
51
52
    /**
53
     * @param string $cdbXmlNamespaceUri
54
     * @param string $cdbXml
55
     * @param string $eventId
56
     * @throws \CultureFeed_Cdb_ParseException
57
     */
58
    private function dispatchCommand(
59
        $cdbXmlNamespaceUri,
60
        $cdbXml,
61
        $eventId
62
    ) {
63
        $event = EventItemFactory::createEventFromCdbXml(
64
            $cdbXmlNamespaceUri,
65
            $cdbXml
66
        );
67
68
        // Location is required, else the create would fail.
69
        // This location needs to be a dummy location.
70
        // A dummy location has no cdbid and no external id.
71
        $location = $event->getLocation();
72
        if ($location->getCdbid() || $location->getExternalId()) {
73
            return;
74
        }
75
76
        // Address is required, else the create would fail.
77
        $physicalAddress = $location->getAddress()->getPhysicalAddress();
78
        if (!$physicalAddress) {
79
            return;
80
        }
81
82
        // Address is always valid, else the create would fail.
83
        $address = $this->addressFactory->fromCdbAddress($physicalAddress);
84
85
        $command = new UpdateGeoCoordinatesFromAddress(
86
            $eventId,
87
            $address
88
        );
89
90
        $this->commandBus->dispatch($command);
91
    }
92
}
93