Test Setup Failed
Push — master ( d14681...b3a262 )
by Gabriel
06:28
created

EventPage::getLabelMaps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 2
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sportic\Timing\RaceTecClient\Parsers;
4
5
use DOMElement;
6
use Sportic\Timing\CommonClient\Models\Race;
7
use Sportic\Timing\CommonClient\Models\Result;
8
9
/**
10
 * Class EventPage
11
 * @package Sportic\Timing\RaceTecClient\Parsers
12
 */
13
class EventPage extends AbstractParser
14
{
15
    protected $returnContent = [];
16
17
    /**
18
     * @return array
19
     */
20
    protected function generateContent()
21
    {
22
        $this->returnContent['races']                 = $this->parseRaces();
23
        $this->returnContent['results']['header']     = $this->parseResultsHeader();
24
        $this->returnContent['results']['list']       = $this->parseResultsTable();
25
        $this->returnContent['results']['pagination'] = $this->parseResultsPagination();
26
27
        return $this->returnContent;
28
    }
29
30
    public function getModelClassName()
31
    {
32
        // TODO: Implement getModelClassName() method.
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    protected function parseRaces()
39
    {
40
        $return    = [];
41
        $eventMenu = $this->getCrawler()->filter('#ctl00_Content_Main_pnlEventMenu');
42
        if ($eventMenu->count() > 0) {
43
            $raceLinks = $eventMenu->filter('div.tab > a');
44
            foreach ($raceLinks as $link) {
45
                $parameters = [
46
                    'name' => $link->nodeValue,
47
                    'href' => $link->getAttribute('href')
48
                ];
49
                $return[]   = new Race($parameters);
50
            }
51
        }
52
53
        return $return;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    protected function parseResultsTable()
60
    {
61
        $return      = [];
62
        $resultsRows = $this->getCrawler()->filter(
63
            '#ctl00_Content_Main_grdNew_DXMainTable > tbody > tr'
64
        );
65
        if ($resultsRows->count() > 0) {
66
            foreach ($resultsRows as $resultRow) {
67
                if ($resultRow->getAttribute('id') !== 'ctl00_Content_Main_grdNew_DXHeadersRow') {
68
                    $result = $this->parseResultsRow($resultRow);
69
                    if ($result) {
70
                        $return[] = $result;
71
                    }
72
                }
73
            }
74
        }
75
76
        return $return;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    protected function parseResultsHeader()
83
    {
84
        $return = [];
85
86
        $fields   = $this->getCrawler()->filter(
87
            '#ctl00_Content_Main_grdNew_DXHeadersRow table td a'
88
        );
89
        $fieldMap = self::getLabelMaps();
90
        if ($fields->count() > 0) {
91
            $colNum = 0;
92 View Code Duplication
            foreach ($fields as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
                $fieldName = $field->nodeValue;
94
                $labelFind = array_search($fieldName, $fieldMap);
95
                if ($labelFind) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $labelFind of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
96
                    $return[$colNum] = $labelFind;
97
                }
98
                $colNum++;
99
            }
100
        }
101
102
        return $return;
103
    }
104
105
    /**
106
     * @param DOMElement $row
107
     *
108
     * @return bool|Result
109
     */
110 View Code Duplication
    protected function parseResultsRow(DOMElement $row)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $parameters = [];
113
        $i          = 0;
114
        foreach ($row->childNodes as $cell) {
115
            if ($cell instanceof DOMElement) {
116
                $parameters = $this->parseResultsRowCell($i, $cell, $parameters);
117
                $i++;
118
            }
119
        }
120
        if (count($parameters)) {
121
            return new Result($parameters);
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * @param int $colCount
129
     * @param DOMElement $cell
130
     *
131
     * @param array $parameters
132
     *
133
     * @return array
134
     */
135
    protected function parseResultsRowCell($colCount, DOMElement $cell, $parameters = [])
136
    {
137
        if (isset($this->returnContent['results']['header'][$colCount])) {
138
            $field = $this->returnContent['results']['header'][$colCount];
139
            if ($field == 'fullName') {
140
                $parameters['href'] = $cell->firstChild->getAttribute('href');
141
                $parameters[$field] = trim($cell->nodeValue);
142
            } else {
143
                $parameters[$field] = trim($cell->nodeValue);
144
            }
145
        }
146
147
        return $parameters;
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    protected function parseResultsPagination()
154
    {
155
        $return = [
156
            'current' => 1,
157
            'all'     => 1,
158
            'items'   => 1,
159
        ];
160
161
        $paginationObject = $this->getCrawler()->filter(
162
            '#ctl00_Content_Main_lblTopPager'
163
        );
164
165
        if ($paginationObject->count() > 0) {
166
            $elements          = explode(' ', $paginationObject->html());
167
            $return['current'] = intval($elements[1]);
168
            $return['all']     = intval($elements[3]);
169
            $return['items']   = intval(str_replace('(', '', $elements[4]));
170
        }
171
172
        return $return;
173
    }
174
175
    /**
176
     * @return array
177
     */
178
    public static function getLabelMaps()
179
    {
180
        return [
181
            'posGen'      => 'Pos',
182
            'bib'         => 'Race No',
183
            'fullName'    => 'Name',
184
            'time'        => 'Time',
185
            'category'    => 'Category',
186
            'posCategory' => 'Cat Pos',
187
            'gender'      => 'Gender',
188
            'posGender'   => 'Gen Pos'
189
        ];
190
    }
191
}
192