1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright Andrea Heigl <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licenses under the MIT-license. For details see the included file LICENSE.md |
9
|
|
|
*/ |
10
|
|
|
namespace Callingallpapers\Subcommands\Sessionize\Parser; |
11
|
|
|
|
12
|
|
|
use Callingallpapers\Entity\Cfp; |
13
|
|
|
use Callingallpapers\Service\ServiceContainer; |
14
|
|
|
use Callingallpapers\Service\TimezoneService; |
15
|
|
|
use DateTimeZone; |
16
|
|
|
use DOMDocument; |
17
|
|
|
use DOMXPath; |
18
|
|
|
use Exception; |
19
|
|
|
use GuzzleHttp\Client; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
use Throwable; |
22
|
|
|
|
23
|
|
|
class EntryParser |
24
|
|
|
{ |
25
|
|
|
private $cfp; |
26
|
|
|
|
27
|
|
|
private $timezoneService; |
28
|
|
|
|
29
|
|
|
private $geolocationService; |
30
|
|
|
|
31
|
|
|
public function __construct(Cfp $cfp, ServiceContainer $container) |
32
|
|
|
{ |
33
|
|
|
$this->cfp = $cfp; |
34
|
|
|
$this->timezoneService = $container->getTimezoneService(); |
35
|
|
|
$this->geolocationService = $container->getGeolocationService(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function parse($uri): Cfp |
39
|
|
|
{ |
40
|
|
|
$cfp = clone($this->cfp); |
41
|
|
|
try { |
42
|
|
|
$dom = new DOMDocument('1.0', 'UTF-8'); |
43
|
|
|
|
44
|
|
|
$content = file_get_contents($uri); |
45
|
|
|
$content = mb_convert_encoding($content, 'UTF-8'); |
46
|
|
|
$content = preg_replace('/\<!DOCTYPE[^\>]*?\>/im', '', $content); |
47
|
|
|
|
48
|
|
|
$dom->loadHTML( |
49
|
|
|
'<?xml version="1.0" encoding="UTF-8" ?>' . $content, |
50
|
|
|
LIBXML_NOBLANKS ^ LIBXML_NOERROR ^ LIBXML_NOENT |
51
|
|
|
); |
52
|
|
|
$dom->preserveWhiteSpace = false; |
53
|
|
|
|
54
|
|
|
$xpath = new DOMXPath($dom); |
55
|
|
|
|
56
|
|
|
$eventLocation = new Location(); |
57
|
|
|
$cfp->location = $eventLocation->parse($dom, $xpath); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$location = $this->geolocationService->getLocationForAddress($cfp->location); |
61
|
|
|
$cfp->latitude = $location->getLatitude(); |
62
|
|
|
$cfp->longitude = $location->getLongitude(); |
63
|
|
|
$timezone = $this->timezoneService->getTimezoneForLocation($location->getLatitude(), $location->getLongitude()); |
64
|
|
|
} catch (\UnexpectedValueException $e) { |
65
|
|
|
error_log($e->getMessage()); |
66
|
|
|
} |
67
|
|
|
$cfp->timezone = $timezone; |
|
|
|
|
68
|
|
|
|
69
|
|
|
$timezone = new DateTimeZone($cfp->timezone); |
70
|
|
|
|
71
|
|
|
$closingDateParser = new ClosingDate($timezone); |
72
|
|
|
$cfp->dateEnd = $closingDateParser->parse($dom, $xpath); |
73
|
|
|
|
74
|
|
|
$descriptionParser = new Description(); |
75
|
|
|
$cfp->description = $descriptionParser->parse($dom, $xpath); |
76
|
|
|
|
77
|
|
|
$openingDateParser = new OpeningDate($timezone); |
78
|
|
|
try { |
79
|
|
|
$cfp->dateStart = $openingDateParser->parse($dom, $xpath); |
80
|
|
|
} catch (Exception $e) { |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$cfp->uri = $uri; |
84
|
|
|
|
85
|
|
|
$confNameParser = new EventName(); |
86
|
|
|
$cfp->conferenceName = $confNameParser->parse($dom, $xpath); |
87
|
|
|
|
88
|
|
|
$confUriParser = new EventUri(); |
89
|
|
|
$cfp->conferenceUri = $confUriParser->parse($dom, $xpath); |
90
|
|
|
|
91
|
|
|
$eventStartDate = new EventStartDate($timezone); |
92
|
|
|
$cfp->eventStartDate = $eventStartDate->parse($dom, $xpath); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$eventEndDate = new EventEndDate($timezone); |
96
|
|
|
$cfp->eventEndDate = $eventEndDate->parse($dom, $xpath); |
97
|
|
|
} catch (InvalidArgumentException $e) { |
98
|
|
|
$cfp->eventEndDate = $cfp->eventStartDate; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
try { |
102
|
|
|
$iconUri = new IconUri($uri); |
103
|
|
|
$cfp->iconUri = $iconUri->parse($dom, $xpath); |
104
|
|
|
} catch (Throwable $e) { |
105
|
|
|
$cfp->iconUri = ''; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $cfp; |
109
|
|
|
} catch (Exception $e) { |
110
|
|
|
throw $e; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: