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

ResultPage::getLabelMaps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 2
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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\Split;
7
8
/**
9
 * Class ResultPage
10
 * @package Sportic\Timing\RaceTecClient\Parsers
11
 */
12
class ResultPage extends AbstractParser
13
{
14
    protected $returnContent = [];
15
16
    /**
17
     * @inheritdoc
18
     */
19
    protected function generateContent()
20
    {
21
        $this->returnContent['full_name'] = $this->parseFullName();
22
        $this->returnContent['time']      = $this->parseFinishTime();
23
        $this->parsePositions();
24
        $this->parseResultBio();
25
        $this->returnContent['splits'] = $this->parseSplits();
26
27
        return $this->returnContent;
28
    }
29
30
    public function getModelClassName()
31
    {
32
        // TODO: Implement getModelClassName() method.
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    protected function parseFullName()
39
    {
40
        return trim(
41
            $this->getCrawler()->filter('#ctl00_Content_Main_lblName')->text()
42
        );
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    protected function parseFinishTime()
49
    {
50
        return trim(
51
            $this->getCrawler()->filter('#ctl00_Content_Main_lblResFinishTime')->text()
52
        );
53
    }
54
55
    protected function parsePositions()
56
    {
57
        $posGenData = $this->getCrawler()->filter('#ctl00_Content_Main_lblResOPos')->text();
58
        list($posGen, $participants) = explode('/', $posGenData);
59
        $this->returnContent['pos_gen'] = trim($posGen);
60
        $this->returnContent['race']['participants'] = trim($participants);
61
62
        $posGenData = $this->getCrawler()->filter('#ctl00_Content_Main_lblResGPos')->text();
63
        list($posGen, $participants) = explode('/', $posGenData);
64
        $this->returnContent['pos_gender'] = trim($posGen);
65
        $this->returnContent['gender']['participants'] = trim($participants);
66
67
        $posGenData = $this->getCrawler()->filter('#ctl00_Content_Main_lblResCPos')->text();
68
        list($posGen, $participants) = explode('/', $posGenData);
69
        $this->returnContent['pos_category'] = trim($posGen);
70
        $this->returnContent['category']['participants'] = trim($participants);
71
    }
72
73
    protected function parseResultBio()
74
    {
75
        $table = $this->getCrawler()->filter('#ctl00_Content_Main_grdBio');
76
        $rows = $table->filter('tbody > tr');
77
78
        foreach ($rows as $row) {
79
            $indexCount = $row->childNodes->length;
80
            $column = $row->childNodes->item($indexCount-4)->nodeValue;
81
            $value = $row->childNodes->item($indexCount-2)->nodeValue;
82
83
            switch ($column) {
84
                case 'Race No':
85
                    $this->returnContent['bib'] = $value;
86
                    break;
87
                case 'Gender':
88
                    $this->returnContent['gender']['name'] = ($value == 'Female' ? 'female' : 'male');
89
                    break;
90
                case 'Category':
91
                    $this->returnContent['category']['name'] = $value;
92
                    break;
93
                case 'Status':
94
                    $this->returnContent['status']['name'] = $value;
95
                    break;
96
            }
97
        }
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    protected function parseSplits()
104
    {
105
        $return     = [];
106
        $headerData = [];
107
        $splitRows  = $this->getCrawler()->filter(
108
            '#ctl00_Content_Main_grdSplits_DXMainTable > tbody > tr'
109
        );
110
        if ($splitRows->count() > 0) {
111
            foreach ($splitRows as $resultRow) {
112
                if ($resultRow->getAttribute('id') === 'ctl00_Content_Main_grdSplits_DXHeadersRow') {
113
                    $headerData = $this->parseSplitsHeader($resultRow);
114
                } else {
115
                    $result = $this->parseSplitRow($resultRow, $headerData);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->parseSplitRow($resultRow, $headerData) (which targets Sportic\Timing\RaceTecCl...ltPage::parseSplitRow()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
116
                    if ($result) {
117
                        $return[] = $result;
118
                    }
119
120
                }
121
            }
122
        }
123
        return $return;
124
    }
125
126
    /**
127
     * @param DOMElement $row
128
     *
129
     * @return array
130
     */
131
    protected function parseSplitsHeader($row)
132
    {
133
        $return = [];
134
135
        $fieldMap = self::getLabelMaps();
136
        $colNum   = 0;
137 View Code Duplication
        foreach ($row->childNodes as $node) {
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...
138
            if ($node instanceof DOMElement) {
139
                $fieldName = trim($node->nodeValue);
140
                $labelFind = array_search($fieldName, $fieldMap);
141
                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...
142
                    $return[$colNum] = $labelFind;
143
                }
144
                $colNum++;
145
            }
146
        }
147
148
        return $return;
149
    }
150
151
    /**
152
     * @param $row
153
     * @param $headerData
154
     *
155
     * @return Split|null
156
     */
157 View Code Duplication
    protected function parseSplitRow($row, $headerData)
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...
158
    {
159
        $parameters = [];
160
        $i          = 0;
161
        foreach ($row->childNodes as $cell) {
162
            if ($cell instanceof DOMElement) {
163
                $parameters = $this->parseSplitCell($i, $cell, $headerData, $parameters);
164
                $i++;
165
            }
166
        }
167
        if (count($parameters)) {
168
            return new Split($parameters);
169
        }
170
171
        return null;
172
    }
173
174
175
    /**
176
     * @param $colCount
177
     * @param $cell
178
     * @param $headerData
179
     * @param $parameters
180
     *
181
     * @return array
182
     */
183
    protected function parseSplitCell($colCount, $cell, $headerData, $parameters)
184
    {
185
        if (isset($headerData[$colCount])) {
186
            $field              = $headerData[$colCount];
187
            $parameters[$field] = trim($cell->nodeValue);
188
        }
189
190
        return $parameters;
191
    }
192
    /**
193
     * @return array
194
     */
195
    protected static function getLabelMaps()
196
    {
197
        return [
198
            'name' => 'Split Name',
199
            'timeFromStart' => 'Time',
200
            'time' => 'Time From Previous Split',
201
        ];
202
    }
203
}
204