Passed
Push — master ( 850a40...1713f1 )
by Rougin
02:20
created

Client::redirected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pilipinews\Website\Sunstar;
4
5
use Nacmartin\PhpExecJs\PhpExecJs;
6
use Nacmartin\PhpExecJs\Runtime\ExternalRuntime;
7
use Pilipinews\Common\Client as CurlClient;
8
9
class Client
10
{
11
    /**
12
     * @var \Nacmartin\PhpExecJs\PhpExecJs
13
     */
14
    protected $executor;
15
16
    /**
17
     * Initializes the cURL session.
18
     */
19 33
    public function __construct()
20
    {
21 33
        $binaries = array('node', 'nodejs');
22
23 33
        $runtime = new ExternalRuntime(null, $binaries);
24
25 33
        $this->executor = new PhpExecJs($runtime);
26 33
    }
27
28
    /**
29
     * Performs the HTTP request based on the given URL.
30
     *
31
     * @param  string $url
32
     * @return string
33
     */
34 33
    public static function request($url)
35
    {
36 33
        $client = new CurlClient;
37
38 33
        $self = new static;
39
40 33
        $client->url($url);
41
42 33
        $result = $client->execute(false);
43
44 33
        echo json_encode($result) . PHP_EOL;
45
46 33
        if ($self->redirected($result)) {
47
            $cookie = $client->cookie($matches[1]);
0 ignored issues
show
Bug introduced by
The method cookie() does not exist on Pilipinews\Common\Client. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            /** @scrutinizer ignore-call */ 
48
            $cookie = $client->cookie($matches[1]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Comprehensibility Best Practice introduced by
The variable $matches seems to be never defined.
Loading history...
48
49
            $client->set(CURLOPT_COOKIE, $cookie);
50
        }
51
52 33
        return $client->execute();
53
    }
54
55
    /**
56
     * Returns the cookie value based on given script.
57
     *
58
     * @param  string $result
59
     * @return string
60
     */
61
    protected function cookie($result)
62
    {
63
        $pattern = '/<script>(.*?)<\/script>/i';
64
65
        preg_match($pattern, $result, $matches);
66
67
        echo json_encode($matches) . PHP_EOL;
68
69
        $script = str_replace('e(r);', 'r', $result);
70
71
        $eval = $this->executor->evalJs($script);
72
73
        $search = array('document.cookie=', 'location.reload()');
74
75
        $script = str_replace($search, array('x=', 'x'), $eval);
76
77
        return $this->executor->evalJs((string) $script);
78
    }
79
80
    /**
81
     * Checks if the result is being redirected.
82
     *
83
     * @param  string $result
84
     * @return boolean
85
     */
86 33
    protected function redirected($result)
87
    {
88 33
        return strpos($result, 'You are being redirected') !== false;
89
    }
90
}
91