Completed
Pull Request — develop (#584)
by Alejandro
05:15
created

UrlValidator::doValidateUrl()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 15
rs 10
ccs 7
cts 7
cp 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Util;
6
7
use Fig\Http\Message\RequestMethodInterface;
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\Exception\GuzzleException;
10
use GuzzleHttp\RequestOptions;
11
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
12
13
class UrlValidator implements UrlValidatorInterface, RequestMethodInterface
14
{
15
    private const MAX_REDIRECTS = 15;
16
17
    /** @var ClientInterface */
18
    private $httpClient;
19
20 2
    public function __construct(ClientInterface $httpClient)
21
    {
22 2
        $this->httpClient = $httpClient;
23
    }
24
25
    /**
26
     * @throws InvalidUrlException
27
     */
28 2
    public function validateUrl(string $url): void
29
    {
30
        try {
31 2
            $this->httpClient->request(self::METHOD_GET, $url, [
32 2
                RequestOptions::ALLOW_REDIRECTS => ['max' => self::MAX_REDIRECTS],
33
            ]);
34 1
        } catch (GuzzleException $e) {
35 1
            throw InvalidUrlException::fromUrl($url, $e);
36
        }
37
    }
38
}
39