EventItemFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Cdb;
4
5
use CultureFeed_Cdb_Data_Keyword;
6
use CultureFeed_Cdb_Item_Event;
7
use CultureFeed_Cdb_ParseException;
8
use SimpleXMLElement;
9
10
class EventItemFactory implements EventItemFactoryInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $namespaceUri;
16
17
    /**
18
     * @param string $namespaceUri
19
     */
20
    public function __construct($namespaceUri)
21
    {
22
        $this->namespaceUri = $namespaceUri;
23
    }
24
25
    /**
26
     * @param string $cdbXml
27
     * @throws \CultureFeed_Cdb_ParseException
28
     * @return \CultureFeed_Cdb_Item_Event
29
     */
30
    public function createFromCdbXml($cdbXml)
31
    {
32
        return self::createEventFromCdbXml($this->namespaceUri, $cdbXml);
33
    }
34
35
    /**
36
     * @param string $namespaceUri
37
     * @param string $cdbXml
38
     * @throws CultureFeed_Cdb_ParseException
39
     * @return CultureFeed_Cdb_Item_Event
40
     */
41
    public static function createEventFromCdbXml($namespaceUri, $cdbXml)
42
    {
43
        $udb2SimpleXml = new SimpleXMLElement(
44
            $cdbXml,
45
            0,
46
            false,
47
            $namespaceUri
48
        );
49
50
        // The event might be wrapped in a <cdbxml> tag.
51
        if ($udb2SimpleXml->getName() == 'cdbxml' && isset($udb2SimpleXml->event)) {
52
            $udb2SimpleXml = $udb2SimpleXml->event;
0 ignored issues
show
Bug introduced by
The property event does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
        }
54
55
        $event = CultureFeed_Cdb_Item_Event::parseFromCdbXml($udb2SimpleXml);
56
57
        if (self::isEventOlderThanSplitKeywordFix($event)) {
58
            $event = self::splitKeywordTagOnSemiColon($event);
59
        }
60
61
        return $event;
62
    }
63
64
    /**
65
     * UDB2 contained a bug that allowed for a keyword to have a semicolon.
66
     * @param CultureFeed_Cdb_Item_Event $event
67
     * @return CultureFeed_Cdb_Item_Event
68
     */
69
    private static function splitKeywordTagOnSemiColon(
70
        CultureFeed_Cdb_Item_Event $event
71
    ) {
72
        $event = clone $event;
73
74
        /**
75
         * @var CultureFeed_Cdb_Data_Keyword[] $keywords
76
         */
77
        $keywords = $event->getKeywords(true);
78
79
        foreach ($keywords as $keyword) {
80
            $individualKeywords = explode(';', $keyword->getValue());
81
82
            if (count($individualKeywords) > 1) {
83
                $event->deleteKeyword($keyword);
84
85
                foreach ($individualKeywords as $individualKeyword) {
86
                    $newKeyword = new CultureFeed_Cdb_Data_Keyword(
87
                        trim($individualKeyword),
88
                        $keyword->isVisible()
89
                    );
90
                    $event->addKeyword($newKeyword);
91
                }
92
            }
93
        }
94
95
        return $event;
96
    }
97
98
    /**
99
     * @param CultureFeed_Cdb_Item_Event $event
100
     * @return bool
101
     */
102
    private static function isEventOlderThanSplitKeywordFix(
103
        CultureFeed_Cdb_Item_Event $event
104
    ) {
105
        return $event->getLastUpdated() < '2016-03-10T00:00:00';
106
    }
107
}
108