Completed
Pull Request — master (#13)
by Andreas
03:52
created

Entry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * Copyright (c) 2015-2015 Andreas Heigl<[email protected]>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @author    Andreas Heigl<[email protected]>
24
 * @copyright 2015-2015 Andreas Heigl/callingallpapers.com
25
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
26
 * @version   0.0
27
 * @since     06.03.2012
28
 * @link      http://github.com/joindin/callingallpapers
29
 */
30
namespace Callingallpapers\Parser\Lanyrd;
31
32
use Callingallpapers\Entity\Cfp;
33
use GuzzleHttp\Client;
34
35
class Entry
36
{
37
    /** @var  Cfp */
38
    protected $cfp;
39
40
    /** @var Client */
41
    protected $client;
42
43
    public function __construct(Cfp $cfp, Client $client)
44
    {
45
        $this->cfp = $cfp;
46
        $this->client = $client;
47
    }
48
49
    public function parse($uri)
50
    {
51
        $cfp = clone($this->cfp);
52
        try {
53
            $dom = new \DOMDocument('1.0', 'UTF-8');
54
55
            $content = file_Get_contents($uri);
56
            $content = mb_convert_encoding($content, 'UTF-8');
57
            $dom->loadHTML('<?xml version="1.0" charset="UTF-8" ?>' . $content);
58
            $dom->preserveWhiteSpace = false;
59
60
            $xpath = new \DOMXPath($dom);
61
62
            $closingDateParser = new ClosingDate();
63
            $cfp->dateEnd = $closingDateParser->parse($dom, $xpath);
64
65
            $eventPageDom = $this->getEventPage($xpath);
66
            $eventXpath = new \DOMXPath(($eventPageDom));
67
68
            $descriptionParser = new Description();
69
            $cfp->description = $descriptionParser->parse($dom, $xpath);
70
71
            $openingDateParser = new OpeningDate();
72
            $cfp->dateStart = $openingDateParser->parse($dom, $xpath);
73
74
            $cfpUriParser = new Uri();
75
            $cfp->uri = $cfpUriParser->parse($dom, $xpath);
76
77
            $confNameParser = new EventName();
78
            $cfp->conferenceName = $confNameParser->parse($dom, $xpath);
79
80
            $confUriParser = new EventUri();
81
            $cfp->conferenceUri  = $confUriParser->parse($eventPageDom, $eventXpath);
82
83
            $eventStartDate = new EventStartDate();
84
            $cfp->eventStartDate = $eventStartDate->parse($dom, $xpath);
85
86
            try {
87
                $eventEndDate      = new EventEndDate();
88
                $cfp->eventEndDate = $eventEndDate->parse($dom, $xpath);
89
            } catch (\InvalidArgumentException $e) {
90
                $cfp->eventEndDate = $cfp->eventStartDate;
91
            }
92
93
            $eventLocation = new Location();
94
            $cfp->location = $eventLocation->parse($dom, $xpath);
95
96
            try {
97
                $location = $this->getLatLonForLocation($cfp->location);
98
                $cfp->latitude = $location[0];
0 ignored issues
show
Documentation Bug introduced by
The property $latitude was declared of type double, but $location[0] is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
99
                $cfp->longitude = $location[1];
0 ignored issues
show
Documentation Bug introduced by
The property $longitude was declared of type double, but $location[1] is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
100
            } catch (\UnexpectedValueException $e) {
101
                error_log($e->getMessage());
102
            }
103
104
            try {
105
                $tags = new Tags();
106
                $cfp->tags = $tags->parse($eventPageDom, $eventXpath);
107
            } catch (\InvalidArgumentException $e) {
108
                $cfp->tags = [];
109
            }
110
111
            return $cfp;
112
        } catch (\Exception $e) {
113
            throw $e;
114
        }
115
    }
116
117
    protected function getLatLonForLocation($location)
118
    {
119
        $result = $this->client->get(sprintf(
120
            'https://nominatim.openstreetmap.org/search?q=%1$s&format=json',
121
            urlencode($location)
122
        ));
123
124
        $locations = json_decode($result->getBody()->getContents());
125
126
        if (empty($locations)) {
127
            return [0, 0];
128
        }
129
        $location = $locations[0];
130
131
        return [$location->lat, $location->lon];
132
    }
133
134
    public function getEventPage($xpath)
135
    {
136
        $confPath = $xpath->query("//h3/a[contains(@class, 'summary')]");
137
138
        if (! $confPath || $confPath->length == 0) {
139
            throw new \InvalidArgumentException('We can\'t find an EventPage');
140
        }
141
142
        $dom = new \DOMDocument('1.0', 'UTF-8');
143
144
        $content = file_get_contents('https://lanyrd.com' . $confPath->item(0)->attributes->getNamedItem('href')->textContent);
145
        $content = mb_convert_encoding($content, 'UTF-8');
146
        $dom->loadHTML('<?xml version="1.0" charset="UTF-8" ?>' . $content);
147
        $dom->preserveWhiteSpace = false;
148
149
        return $dom;
150
    }
151
}
152