Parser   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
c 4
b 0
f 0
lcom 0
cbo 2
dl 0
loc 93
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllLessons() 0 17 4
A hasNextPage() 0 11 2
A getToken() 0 6 1
A getDownloadLink() 0 10 2
A getNameOfEpisode() 0 7 1
1
<?php
2
/**
3
 * Dom Parser
4
 */
5
namespace App\Html;
6
7
use App\Exceptions\NoDownloadLinkException;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
/**
11
 * Class Parser
12
 * @package App\Html
13
 */
14
class Parser
15
{
16
    /**
17
     * Parses the html and adds the lessons the the array.
18
     *
19
     * @param $html
20
     * @param $array
21
     */
22
    public static function getAllLessons($html, &$array)
23
    {
24
        $parser = new Crawler($html);
25
26
        $parser->filter('h5.lesson-list-title')->each(function (Crawler $node) use (&$array) {
27
            $link = $node->children()->attr('href');
28
            if (preg_match('/'.LARACASTS_LESSONS_PATH.'\/(.+)/', $link, $matches)) { // lesson
29
                $array['lessons'][] = $matches[1];
30
            } else if ($node->children()->count() > 0) {
31
                $link = $node->children()->eq(0)->attr('href');
32
33
                if (preg_match('/'.LARACASTS_SERIES_PATH.'\/(.+)\/episodes\/(\d+)/', $link, $matches)) { // serie lesson
34
                    $array['series'][$matches[1]][] = (int) $matches[2];
35
                }
36
            }
37
        });
38
    }
39
40
    /**
41
     * Determines if there is next page, false if not or the link.
42
     *
43
     * @param $html
44
     *
45
     * @return bool|string
46
     */
47
    public static function hasNextPage($html)
48
    {
49
        $parser = new Crawler($html);
50
51
        $node = $parser->filter('[rel=next]');
52
        if ($node->count() > 0) {
53
            return $node->attr('href');
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * Gets the token input.
61
     *
62
     * @param $html
63
     *
64
     * @return string
65
     */
66
    public static function getToken($html)
67
    {
68
        $parser = new Crawler($html);
69
70
        return $parser->filter("input[name=_token]")->attr('value');
71
    }
72
73
    /**
74
     * Gets the download link.
75
     *
76
     * @param $html
77
     * @return string
78
     * @throws NoDownloadLinkException
79
     */
80
    public static function getDownloadLink($html)
81
    {
82
        preg_match("('\/downloads\/.*?')", $html, $matches);
83
84
        if(isset($matches[0]) === false) {
85
            throw new NoDownloadLinkException();
86
        }
87
88
        return LARACASTS_BASE_URL . substr($matches[0],1,-1);
89
    }
90
91
    /**
92
     * Extracts the name of the episode.
93
     *
94
     * @param $html
95
     *
96
     * @param $path
97
     * @return string
98
     */
99
    public static function getNameOfEpisode($html, $path)
100
    {
101
        $parser = new Crawler($html);
102
        $t = $parser->filter("a[href='/".$path."']")->text();
103
104
        return trim($t);
105
    }
106
}
107