Completed
Push — master ( e21061...77fa8f )
by
unknown
02:33
created

PrerenderClient::getRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
3
namespace MediaMonks\Crawler\Client;
4
5
use Goutte\Client as BaseClient;
6
use Symfony\Component\BrowserKit\History;
7
use Symfony\Component\BrowserKit\CookieJar;
8
use Symfony\Component\BrowserKit\Request;
9
10
class PrerenderClient extends BaseClient
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $prerenderUrl;
16
17
    /**
18
     * @param string $prerenderUrl The url of the prerender server
19
     * @param array $server The server parameters (equivalent of $_SERVER)
20
     * @param History $history A History instance to store the browser history
21
     * @param CookieJar $cookieJar A CookieJar instance to store the cookies
22
     */
23 2
    public function __construct($prerenderUrl, array $server = [], History $history = null, CookieJar $cookieJar = null)
24
    {
25 2
        $this->prerenderUrl = $prerenderUrl;
26
27 2
        parent::__construct($server, $history, $cookieJar);
28 2
    }
29
30
    /**
31
     * @param string $uri
32
     * @return string
33
     */
34 2
    protected function getAbsoluteUri($uri)
35
    {
36 2
        return $this->prerenderUrl.parent::getAbsoluteUri($uri);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function getRequest()
43
    {
44
        $request = parent::getRequest();
45
        if (!empty($request)) {
46
            return new Request($this->correctUrl($request->getUri()),
47
                $request->getMethod(), $request->getParameters(),
48
                $request->getFiles(), $request->getCookies(), $request->getServer(),
49
                $request->getContent());
50
        }
51
    }
52
53
    /**
54
     * @param $url
55
     *
56
     * @return string
57
     */
58
    protected function correctUrl($url)
59
    {
60
        return str_replace($this->prerenderUrl, '', $url);
61
    }
62
}
63