Client::redirected()   A
last analyzed

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 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\Website\Sunstar\Script
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;
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 11
        {
48
            $pattern = '/<script>(.*?)<\/script>/i';
49
50
            preg_match($pattern, $result, $matches);
51
52
            $cookie = $self->cookie($matches[1]);
53
54
            $self->set(CURLOPT_COOKIE, $cookie);
55
        }
56
57 33
        return $self->execute();
58
    }
59
60
    /**
61
     * Returns the cookie value based on given script.
62
     *
63
     * @param  string $result
64
     * @return string
65
     */
66
    protected function cookie($result)
67
    {
68
        $script = str_replace('e(r);', 'r', $result);
69
70
        $eval = $this->evaluator->evaluate($script);
71
72
        $search = array('document.cookie=', 'location.reload()');
73
74
        $script = str_replace($search, array('x=', 'x'), $eval);
75
76
        return $this->evaluator->evaluate((string) $script);
77
    }
78
79
    /**
80
     * Checks if the result is being redirected.
81
     *
82
     * @param  string $result
83
     * @return boolean
84
     */
85 33
    protected function redirected($result)
86
    {
87 33
        return strpos($result, 'You are being redirected') !== false;
88
    }
89
}
90