Client   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 8.22 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 7
dl 6
loc 73
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getLocations() 0 15 2
B validateResponse() 6 16 8
A sendRequest() 0 5 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
2
3
namespace AlexeyKuperhstokh\LocationBundle\Client;
4
5
use AlexeyKuperhstokh\LocationBundle\Exceptions\MalformedDataException;
6
use AlexeyKuperhstokh\LocationBundle\Exceptions\ServerErrorException;
7
use AlexeyKuperhstokh\LocationBundle\Location\Location;
8
use GuzzleHttp\ClientInterface;
9
use Psr\Http\Message\RequestInterface;
10
use Symfony\Component\Serializer\Encoder\JsonEncoder;
11
use Symfony\Component\Serializer\Serializer;
12
13
class Client
14
{
15
    /**
16
     * @var ClientInterface
17
     */
18
    protected $httpClient;
19
20
    /**
21
     * @var RequestInterface
22
     */
23
    protected $request;
24
25
    /**
26
     * Client constructor.
27
     * @param ClientInterface $httpClient
28
     * @param RequestInterface $request
29
     */
30
    public function __construct(ClientInterface $httpClient, RequestInterface $request)
31
    {
32
        $this->httpClient = $httpClient;
33
        $this->request = $request;
34
    }
35
36
    /**
37
     * @return Location[]
38
     */
39
    public function getLocations()
40
    {
41
        $body = $this->sendRequest();
42
        $serializer = new Serializer([], [new JsonEncoder()]);
43
        $decoded = $serializer->decode($body, 'json');
44
45
        $this->validateResponse($decoded);
46
47
        $locations = array();
48
        foreach ($decoded['data']['locations'] as $jsonLocation) {
49
            $locations[] = new Location($jsonLocation);
50
        }
51
52
        return $locations;
53
    }
54
55
    /**
56
     * @param $decoded
57
     * @throws MalformedDataException
58
     * @throws ServerErrorException
59
     */
60
    public function validateResponse($decoded)
61
    {
62 View Code Duplication
        if (!isset($decoded['success'], $decoded['data']) || !is_array($decoded['data'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
            throw new MalformedDataException();
64
        }
65
66
        if (!$decoded['success']) {
67
            $message = isset($decoded['data']['message']) ? $decoded['data']['message'] : null;
68
            $code = isset($decoded['data']['code']) ? $decoded['data']['code'] : null;
69
            throw new ServerErrorException($message, $code);
70
        }
71
72 View Code Duplication
        if (!isset($decoded['data']['locations']) || !is_array($decoded['data']['locations'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
            throw new MalformedDataException();
74
        }
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function sendRequest()
81
    {
82
        $response = $this->httpClient->send($this->request);
83
        return $response->getBody()->getContents();
84
    }
85
}
86