BalancerUrlResponseHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 1
A validate() 0 16 3
A parseUrl() 0 11 2
1
<?php
2
3
namespace ninjacto\OrtcPhp\Handlers;
4
5
use GuzzleHttp\Message\FutureResponse;
6
use ninjacto\OrtcPhp\Exceptions\InvalidBalancerUrlException;
7
use ninjacto\OrtcPhp\Models\Responses\BalancerUrlResponse;
8
9
class BalancerUrlResponseHandler extends OrtcResponseHandler
10
{
11
    /**
12
     * handle response from guzzle.
13
     *
14
     * @param FutureResponse $response
15
     *
16
     * @return BalancerUrlResponse
17
     */
18 5
    public function handle($response)
19
    {
20 5
        $body = trim((string) $response);
21
22 5
        $url = $this->parseUrl($body);
23 4
        $this->validate($url);
24
25 1
        $balancedUrlResponse = new BalancerUrlResponse();
26 1
        $balancedUrlResponse->setUrl($url);
27
28 1
        return $balancedUrlResponse;
29
    }
30
31
    /**
32
     * validating response.
33
     *
34
     * @param string $url
35
     *
36
     * @throws InvalidBalancerUrlException
37
     */
38 4
    public function validate($url)
39
    {
40 4
        if (preg_match('/https?:\/\/undefined(:undefined)?/', $url)) {
41 2
            $invalidBalancerUrlException = new InvalidBalancerUrlException();
42 2
            $invalidBalancerUrlException->setUrl($url);
43
44 2
            throw $invalidBalancerUrlException;
45
        }
46
47 2
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
48 1
            $invalidBalancerUrlException = new InvalidBalancerUrlException();
49 1
            $invalidBalancerUrlException->setUrl($url);
50
51 1
            throw $invalidBalancerUrlException;
52
        }
53 1
    }
54
55
    /**
56
     * parse body to find url.
57
     *
58
     * @param string $body
59
     *
60
     * @throws InvalidBalancerUrlException
61
     *
62
     * @return string
63
     */
64 5
    public function parseUrl($body)
65
    {
66 5
        if (!preg_match('/https?:\/\/[^\"]+/', $body, $matches)) {
67 1
            $invalidBalancerUrlException = new InvalidBalancerUrlException();
68 1
            $invalidBalancerUrlException->setUrl($body);
69
70 1
            throw $invalidBalancerUrlException;
71
        }
72
73 4
        return trim($matches[0]);
74
    }
75
}
76