Completed
Push — master ( ad24bf...d452c0 )
by Ricardo
168:19 queued 133:59
created

Validator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 28.3 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 15
loc 53
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setHttpClient() 0 4 1
A validateUrl() 8 8 1
A validateContent() 7 7 1
A normaliseUrl() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace RicardoFiorani\Validator;
4
5
use GuzzleHttp\Psr7\Request;
6
use Psr\Http\Client\ClientInterface;
7
use RicardoFiorani\Validator\Response\ValidationResponseFactory;
8
use RicardoFiorani\Validator\Response\ValidationResponseInterface;
9
10
class Validator implements ValidatorInterface
11
{
12
    const CLOUDFLARE_AMP_VALIDATOR_ENDPOINT = 'https://amp.cloudflare.com/q/';
13
14
    /**
15
     * @var ClientInterface
16
     */
17
    private $httpClient;
18
19
    /**
20
     * @var ValidationResponseFactory
21
     */
22
    private $responseFactory;
23
24 4
    public function __construct(ClientInterface $httpClient)
25
    {
26 4
        $this->setHttpClient($httpClient);
27 4
        $this->responseFactory = new ValidationResponseFactory();
28 4
    }
29
30 4
    public function setHttpClient(ClientInterface $httpClient)
31
    {
32 4
        $this->httpClient = $httpClient;
33 4
    }
34
35
    /**
36
     * @throws \Psr\Http\Client\ClientExceptionInterface
37
     */
38 2 View Code Duplication
    public function validateUrl(string $url): ValidationResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40 2
        $url = $this->normaliseUrl($url);
41 2
        $request = new Request('GET', self::CLOUDFLARE_AMP_VALIDATOR_ENDPOINT . $url);
42 2
        $response = $this->httpClient->sendRequest($request);
43
44 2
        return $this->responseFactory->create($response);
45
    }
46
47
    /**
48
     * @throws \Psr\Http\Client\ClientExceptionInterface
49
     */
50 2 View Code Duplication
    public function validateContent(string $content): ValidationResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52 2
        $request = new Request('POST', self::CLOUDFLARE_AMP_VALIDATOR_ENDPOINT, [], $content);
53 2
        $response = $this->httpClient->sendRequest($request);
54
55 2
        return $this->responseFactory->create($response);
56
    }
57
58 2
    private function normaliseUrl(string $url): string
59
    {
60 2
        return str_replace(['http://', 'https://'], '', strtolower($url));
61
    }
62
}
63