Passed
Push — master ( af6eb6...d676d9 )
by Rougin
02:15
created

Client::request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 21
ccs 12
cts 12
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Pilipinews\Website\Sunstar;
4
5
use Pilipinews\Common\Client as CurlClient;
6
7
/**
8
 * Sunstar cURL Client
9
 *
10
 * @package Pilipinews
11
 * @author  Rougin Gutib <[email protected]>
12
 */
13
class Client extends CurlClient
14
{
15
    /**
16
     * @var \Pilipinews\Sunstar\Script
0 ignored issues
show
Bug introduced by
The type Pilipinews\Sunstar\Script was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
     */
18
    protected $evaluator;
19
20
    /**
21
     * Initializes the cURL session.
22
     */
23 33
    public function __construct()
24
    {
25 33
        parent::__construct();
26
27 33
        $this->evaluator = new Script;
0 ignored issues
show
Documentation Bug introduced by
It seems like new Pilipinews\Website\Sunstar\Script() of type Pilipinews\Website\Sunstar\Script is incompatible with the declared type Pilipinews\Sunstar\Script of property $evaluator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28 33
    }
29
30
    /**
31
     * Performs the HTTP request based on the given URL.
32
     *
33
     * @param  string $url
34
     * @return string
35
     */
36 33
    public static function request($url)
37
    {
38 33
        $self = new static;
39
40 33
        $self->set(CURLOPT_SSL_VERIFYPEER, 0);
41
42 33
        $self->url($url);
43
44 33
        $result = $self->execute(false);
45
46 33
        if ($self->redirected($result)) {
47 33
            $pattern = '/<script>(.*?)<\/script>/i';
48
49 33
            preg_match($pattern, $result, $matches);
50
51 33
            $cookie = $self->cookie($matches[1]);
52
53 33
            $self->set(CURLOPT_COOKIE, $cookie);
54 22
        }
55
56 33
        return $self->execute();
57
    }
58
59
    /**
60
     * Returns the cookie value based on given script.
61
     *
62
     * @param  string $result
63
     * @return string
64
     */
65 33
    protected function cookie($result)
66
    {
67 33
        $script = str_replace('e(r);', 'r', $result);
68
69 33
        $eval = $this->evaluator->evaluate($script);
70
71 33
        $search = array('document.cookie=', 'location.reload()');
72
73 33
        $script = str_replace($search, array('x=', 'x'), $eval);
74
75 33
        return $this->evaluator->evaluate((string) $script);
76
    }
77
78
    /**
79
     * Checks if the result is being redirected.
80
     *
81
     * @param  string $result
82
     * @return boolean
83
     */
84 33
    protected function redirected($result)
85
    {
86 33
        return strpos($result, 'You are being redirected') !== false;
87
    }
88
}
89