Passed
Push — master ( 3f26c1...e5a362 )
by Nicolas
04:09 queued 01:57
created

PlanParser::parseToJson()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 19
cts 19
cp 1
rs 8.5806
cc 4
eloc 17
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Cp\Parser;
4
5
use Cp\Http\HeaderParser;
6
use PHPHtmlParser\Dom;
7
8
/**
9
 * Class PlanParser
10
 */
11
class PlanParser
12
{
13
    /**
14
     * @var Dom
15
     */
16
    private $parser;
17
18
    /**
19
     * @var HeaderParser
20
     */
21
    private $headerParser;
22
23
    /**
24
     * CpParser constructor.
25
     *
26
     * @param Dom          $parser
27
     * @param HeaderParser $headerParser
28
     */
29 3
    public function __construct(Dom $parser, HeaderParser $headerParser)
30
    {
31 3
        $this->parser = $parser;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
32 3
        $this->headerParser = $headerParser;
33 3
    }
34
35
    /**
36
     * @param string $url
37
     *
38
     * @return string
39
     * @throws \Exception
40
     */
41 2
    public function parseToJson($url)
42
    {
43 2
        $htmlContent = file_get_contents($url);
44 2
        $this->generateBadRequestException($url);
45
46 2
        $this->parser->load($htmlContent);
47
48 2
        $weeks = $this->parser->find('#plans table');
49 2
        if (0 >= $weeks->count()) {
50 1
            throw new \Exception(sprintf('Plan not found for this url: %s', $url));
51
        }
52
53 1
        $plan = $this->getPlan();
54 1
        foreach ($weeks as $training) {
0 ignored issues
show
Bug introduced by
The expression $weeks of type array|object<PHPHtmlParser\Dom\AbstractNode> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
55 1
            $week = ['name' => $training->find('thead tr')->find('td')[1]->innerHtml];
56 1
            foreach ($training->find('tbody tr') as $seance) {
57
                $training = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
58 1
                    'type' => strip_tags($seance->find('td')[0]->innerHtml),
59 1
                    'content' => strip_tags($seance->find('td')[1]->innerHtml),
60 1
                ];
61 1
                $week['trainings'][] = $training;
62 1
            }
63 1
            $plan['weeks'][] = $week;
64 1
        }
65
66 1
        return json_encode($plan, true);
67
    }
68
69
    /**
70
     * @param string $url
71
     *
72
     * @return string
73
     * @throws \Exception
74
     */
75 1
    public function parseToHtml($url)
76
    {
77 1
        $htmlContent = file_get_contents($url);
78
79 1
        $this->generateBadRequestException($url);
80 1
        $this->parser->load($htmlContent);
81
82 1
        return sprintf('%s%s%s', '<table>', $this->parser->find('#plans table')->innerHtml, '</table>');
83
    }
84
85
    /**
86
     * @return array
87
     */
88 1
    private function getPlan()
89
    {
90 1
        $nameOfPlan = strip_tags($this->parser->find('.article-content-main h1')->innerHtml);
91 1
        $typeOfPlan = strip_tags($this->parser->find('.article-content-main h3')->innerHtml);
92
93
        return [
94 1
            'name' => strip_tags($nameOfPlan),
95 1
            'type' => strip_tags($typeOfPlan),
96 1
            'weeks' => [],
97 1
        ];
98
    }
99
100
    /**
101
     * @param string $url
102
     *
103
     * @throws \Exception
104
     */
105 3
    public function generateBadRequestException($url)
106
    {
107 3
        if (isset($http_response_header)) {
0 ignored issues
show
Bug introduced by
The variable $http_response_header seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
108
            $responseCode = $this->headerParser->get('response_code', $http_response_header);
109
            if ('200' != $responseCode) {
110
                throw new \Exception(
111
                    sprintf('Url %s return http response code %s', $url, $responseCode),
112
                    $responseCode
113
                );
114
            }
115
        }
116 3
    }
117
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
118