Passed
Push — master ( c5940e...cdf7b9 )
by Nicolas
04:36
created

PlanParser::parseToHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
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 1
    public function __construct(Dom $parser, HeaderParser $headerParser)
30
    {
31 1
        $this->parser = $parser;
32 1
        $this->headerParser = $headerParser;
33 1
    }
34
35
    /**
36
     * @param string $url
37
     *
38
     * @return string
39
     * @throws \Exception
40
     */
41 1
    public function parseToJson($url)
42
    {
43 1
        $htmlContent = file_get_contents($url);
44 1
        $this->generateBadRequestException($url);
45
46 1
        $this->parser->load($htmlContent);
47
48 1
        $weeks = $this->parser->find('#plans table');
49 1
        if (empty($weeks)) {
50
            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 = [
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
    public function parseToHtml($url)
76
    {
77
        $htmlContent = file_get_contents($url);
78
        $this->generateBadRequestException($url);
79
80
        $this->parser->load($htmlContent);
81
82
        return $this->parser->find('#plans table')->innerHtml;
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 1
    private function generateBadRequestException($url)
106
    {
107 1
        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 1
    }
117
}
118