HtmlPageParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 84
ccs 0
cts 30
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parsePunchRow() 0 12 2
A getPageTables() 0 13 2
A getPunchesTableHtml() 0 7 1
A getPunchesRows() 0 20 4
1
<?php
2
namespace Katapoka\Ahgora;
3
4
use DOMDocument;
5
use DOMElement;
6
use InvalidArgumentException;
7
use Katapoka\Ahgora\Contracts\IHttpResponse;
8
9
class HtmlPageParser
10
{
11
    /**
12
     * @param \Katapoka\Ahgora\Contracts\IHttpResponse $punchesPageResponse
13
     *
14
     * @return mixed
15
     */
16
    public function getPunchesTableHtml(IHttpResponse $punchesPageResponse)
17
    {
18
        $tables = $this->getPageTables($punchesPageResponse);
19
20
        //The first table is the data summary
21
        return $tables['punches'];
22
    }
23
24
    /**
25
     * Get the punch's rows in array format.
26
     *
27
     * @param IHttpResponse $punchesPageResponse
28
     *
29
     * @return array
30
     */
31
    public function getPunchesRows(IHttpResponse $punchesPageResponse)
32
    {
33
        $punchesTableHtml = $this->getPunchesTableHtml($punchesPageResponse);
34
35
        $dom = new DOMDocument();
36
        if (!@$dom->loadHTML($punchesTableHtml)) {
37
            throw new InvalidArgumentException('Failed to parse punchesTable');
38
        }
39
40
        $rows = $dom->getElementsByTagName('tr');
41
        $rowsCollection = [];
42
43
        foreach ($rows as $row) {
44
            if ($punchRow = $this->parsePunchRow($row)) {
45
                $rowsCollection[] = $punchRow;
46
            }
47
        }
48
49
        return $rowsCollection;
50
    }
51
52
    /**
53
     * Parse the punch row and return its values.
54
     *
55
     * @param \DOMElement $row
56
     *
57
     * @return array|bool
58
     */
59
    private function parsePunchRow(DOMElement $row)
60
    {
61
        $cols = $row->getElementsByTagName('td');
62
        if ($cols->length !== 8) {
63
            return false;
64
        }
65
66
        return [
67
            'date'   => trim($cols->item(0)->nodeValue),
68
            'punches' => trim($cols->item(2)->nodeValue),
69
        ];
70
    }
71
72
    /**
73
     * Get both tables and return the strings into an array with the properties 'summary' and 'punches'.
74
     *
75
     * @param IHttpResponse $punchesPageResponse
76
     *
77
     * @return array
78
     */
79
    private function getPageTables(IHttpResponse $punchesPageResponse)
80
    {
81
        $regex = '/<table.*?>.*?<\/table>/si';
82
83
        if (!preg_match_all($regex, $punchesPageResponse->getBody(), $matches)) {
84
            throw new InvalidArgumentException('Pattern not found in the response');
85
        }
86
87
        return [
88
            'summary' => $matches[0][0],
89
            'punches'  => $matches[0][1],
90
        ];
91
    }
92
}
93