HtmlPageParser::getPunchesTableHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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