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

Entry::parse()   B

Complexity

Conditions 5
Paths 103

Size

Total Lines 61
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 61
rs 8.5934
cc 5
eloc 42
nc 103
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
            $descriptionParser = new Description();
63
            $cfp->description = $descriptionParser->parse($dom, $xpath);
64
65
            $closingDateParser = new ClosingDate();
66
            $cfp->dateEnd = $closingDateParser->parse($dom, $xpath);
67
68
            $cfpUriParser = new Uri();
69
            $cfp->uri = $cfpUriParser->parse($dom, $xpath);
70
71
            $confNameParser = new EventName();
72
            $cfp->conferenceName = $confNameParser->parse($dom, $xpath);
73
74
            $confUriParser = new EventUri();
75
            $cfp->conferenceUri  = 'http://lanyrd.com/' . $confUriParser->parse($dom, $xpath);
76
77
            $eventStartDate = new EventStartDate();
78
            $cfp->eventStartDate = $eventStartDate->parse($dom, $xpath);
79
80
            try {
81
                $eventEndDate      = new EventEndDate();
82
                $cfp->eventEndDate = $eventEndDate->parse($dom, $xpath);
83
            } catch (\InvalidArgumentException $e) {
84
                $cfp->eventEndDate = $cfp->eventStartDate;
85
            }
86
87
            $eventLocation = new Location();
88
            $cfp->location = $eventLocation->parse($dom, $xpath);
89
90
            try {
91
                $location = $this->getLatLonForLocation($cfp->location);
92
                $cfp->latitude = $location[0];
93
                $cfp->longitude = $location[1];
94
            } catch (\UnexpectedValueException $e) {
95
                error_log($e->getMessage());
96
            }
97
98
            try {
99
                $tags = new Tags();
100
                $cfp->tags = $tags->parse($dom, $xpath);
101
            } catch (\InvalidArgumentException $e) {
102
                $cfp->tags = [];
103
            }
104
105
            return $cfp;
106
        } catch (\Exception $e) {
107
            throw $e;
108
        }
109
    }
110
111
    protected function getLatLonForLocation($location)
112
    {
113
        $result = $this->client->get(sprintf(
114
            'https://nominatim.openstreetmap.org/search?q=%1$s&format=json',
115
            urlencode($location)
116
        ));
117
118
        $locations = json_decode($result->getBody()->getContents());
119
120
        if (empty($locations)) {
121
            throw new \UnexpectedValueException('Not enough items found');
122
        }
123
        $location = $locations[0];
124
125
        return [$location->lat, $location->lon];
126
    }
127
}
128