Completed
Pull Request — master (#9)
by Andreas
02:48
created

Entry::getLatLonForLocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
rs 9.2
cc 2
eloc 12
nc 2
nop 1
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 Geocoder\Exception\UnexpectedValue;
34
use Geocoder\Provider\Nominatim;
35
use GuzzleHttp\Client;
36
use Ivory\HttpAdapter\Configuration;
37
use Ivory\HttpAdapter\CurlHttpAdapter;
38
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
39
40
class Entry
41
{
42
    /** @var  Cfp */
43
    protected $cfp;
44
45
    public function __construct(Cfp $cfp)
46
    {
47
        $this->cfp = $cfp;
48
    }
49
50
    public function parse($uri)
51
    {
52
        $cfp = clone($this->cfp);
53
        try {
54
            $dom = new \DOMDocument('1.0', 'UTF-8');
55
56
            $content = file_Get_contents($uri);
57
            $content = mb_convert_encoding($content, 'UTF-8');
58
            $dom->loadHTML('<?xml version="1.0" charset="UTF-8" ?>' . $content);
59
            $dom->preserveWhiteSpace = false;
60
61
            $xpath = new \DOMXPath($dom);
62
63
            $descriptionParser = new Description();
64
            $cfp->description = $descriptionParser->parse($dom, $xpath);
65
66
            $closingDateParser = new ClosingDate();
67
            $cfp->dateEnd = $closingDateParser->parse($dom, $xpath);
68
69
            $cfpUriParser = new Uri();
70
            $cfp->uri = $cfpUriParser->parse($dom, $xpath);
71
72
            $confNameParser = new EventName();
73
            $cfp->conferenceName = $confNameParser->parse($dom, $xpath);
74
75
            $confUriParser = new EventUri();
76
            $cfp->conferenceUri  = 'http://lanyrd.com/' . $confUriParser->parse($dom, $xpath);
77
78
            $eventStartDate = new EventStartDate();
79
            $cfp->eventStartDate = $eventStartDate->parse($dom, $xpath);
80
81
            try {
82
                $eventEndDate      = new EventEndDate();
83
                $cfp->eventEndDate = $eventEndDate->parse($dom, $xpath);
84
            } catch (\InvalidArgumentException $e) {
85
                $cfp->eventEndDate = $cfp->eventStartDate;
86
            }
87
88
            $eventLocation = new Location();
89
            $cfp->location = $eventLocation->parse($dom, $xpath);
90
91
            try {
92
                $location = $this->getLatLonForLocation($cfp->location);
93
                $cfp->latitude = $location[0];
94
                $cfp->longitude = $location[1];
95
            } catch (\UnexpectedValueException $e) {
96
                error_log($e->getMessage());
97
            }
98
99
            try {
100
                $tags = new Tags();
101
                $cfp->tags = $tags->parse($dom, $xpath);
102
            } catch (\InvalidArgumentException $e) {
103
                $cfp->tags = [];
104
            }
105
106
            return $cfp;
107
        } catch (\Exception $e) {
108
            throw $e;
109
        }
110
    }
111
112
    protected function getLatLonForLocation($location)
113
    {
114
        $client = new Client([
115
            'headers'=> [
116
                'User-Agent' => 'callingallpapers.com - Location to lat/lon-translation - For infos write to [email protected]',
117
            ],
118
        ]);
119
120
        $result = $client->get(sprintf(
121
            'https://nominatim.openstreetmap.org/search?q=%1$s&format=json',
122
            urlencode($location)
123
        ));
124
125
        $locations = json_decode($result->getBody()->getContents());
126
127
        if (empty($locations)) {
128
            throw new \UnexpectedValueException('Not enough items found');
129
        }
130
        $location = $locations[0];
131
132
        return [$location->lat, $location->lon];
133
    }
134
}
135