Completed
Push — master ( 77fa8f...cc9988 )
by
unknown
03:01
created

PrerenderClient::getRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2.0078
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 3
    public function __construct($prerenderUrl, array $server = [], History $history = null, CookieJar $cookieJar = null)
24
    {
25 3
        $this->prerenderUrl = $prerenderUrl;
26
27 3
        parent::__construct($server, $history, $cookieJar);
28 3
    }
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 1
    public function getRequest()
43
    {
44 1
        $request = parent::getRequest();
45 1
        if (!empty($request)) {
46 1
            return new Request($this->correctUrl($request->getUri()),
47 1
                $request->getMethod(), $request->getParameters(),
48 1
                $request->getFiles(), $request->getCookies(), $request->getServer(),
49 1
                $request->getContent());
50
        }
51
    }
52
53
    /**
54
     * @param $url
55
     *
56
     * @return string
57
     */
58 1
    protected function correctUrl($url)
59
    {
60 1
        return str_replace($this->prerenderUrl, '', $url);
61
    }
62
}
63