Client   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 56%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 20
c 8
b 0
f 0
dl 0
loc 75
ccs 14
cts 25
cp 0.56
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A redirected() 0 3 1
A request() 0 22 2
A cookie() 0 11 1
A __construct() 0 5 1
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