Scrapper::parseEvents()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 65
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
c 0
b 0
f 0
rs 8.8507
cc 4
eloc 39
nc 1
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
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Events\Importing;
29
30
use Goutte\Client;
31
use Symfony\Component\DomCrawler\Crawler;
32
33
class Scrapper
34
{
35
    protected $client;
36
37
    public function __construct(Client $client)
38
    {
39
        $this->client = $client;
40
    }
41
42
    /**
43
     * @param string $url
44
     * @return array
45
     */
46
    public function parseEvents($url)
47
    {
48
        $crawler = $this->client->request('GET', $url);
49
50
        $events = [];
51
52
        $crawler->filter('script')->each(function (Crawler $node) use (&$events, $url) {
53
            $text = $node->text();
54
55
            $containers = [
56
                "#events-open-for-applicationcontainer",
57
                "#events-in-progresscontainer",
58
                "#past-eventscontainer"
59
            ];
60
61
            foreach ($containers as $container) {
62
63
                if (strpos($text, $container)) {
64
65
                    $items = explode("'items':", $text);
66
                    $items = $items[1];
67
                    $items = trim($items);
68
69
                    $items = preg_replace('/\s+/', ' ', $items);
70
                    $items = str_replace("'", '"', $items);
71
                    $items = str_replace("], }", "] }", $items);
72
                    $items = str_replace("}, ]", "} ]", $items);
73
                    $items = str_replace("\", ]", "\" ]", $items);
74
                    $items = str_replace("\", }", "\" }", $items);
75
76
                    $length = strlen($items);
77
78
                    $items = substr($items, 0, $length-6);
79
                    $items = json_decode($items, true);
80
81
                    foreach ($items as $item) {
82
83
                        $event = [];
84
85
                        $description =  $item["description"];
86
                        $description = str_replace("\n", "", $description);
87
                        $description = str_replace("\r", "", $description);
88
89
                        $event['title'] = $item['title'];
90
                        $event['organizer'] = trim(explode("by", explode("<hr>", $description)[0])[1]);
91
                        $event['start'] = explode("</date>", explode("<date>", $description)[1])[0];
92
                        $event['end'] =  explode("</date>", explode("till <date>", $description)[1])[0];
93
                        $event['deadline'] = trim(explode("</date>", explode("Application Deadline <date>", $description)[1])[0]);
94
                        $event['image'] = $item['large'][0];
95
                        $event['url'] = $item['button_list'][0]['url'];
96
                        $parts = explode("<hr>", $description);
97
                        $event['details'] = $parts[count($parts) - 1];
98
99
                        $events[] = $event;
100
101
                    }
102
103
                }
104
105
            }
106
107
        });
108
109
        return $events;
110
    }
111
}
112