ApiLoaderTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 119
ccs 0
cts 65
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A resolveFilters() 0 6 1
A apiCall() 0 20 4
A retrieveAll() 0 20 1
A retrieveOne() 0 4 2
A retrieve() 0 12 1
1
<?php
2
3
namespace Majora\Framework\Loader\Bridge\Api;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use GuzzleHttp\Psr7\Response;
7
use Majora\Framework\Api\Client\ApiClientInterface;
8
use Majora\Framework\Loader\LazyLoaderTrait;
9
use Majora\Framework\Loader\LoaderInterface;
10
use Majora\Framework\Loader\LoaderTrait;
11
use Symfony\Component\Serializer\SerializerInterface;
12
13
/**
14
 * Trait to use into Api loaders to get a simple implementation of LoaderInterface.
15
 *
16
 * @property filterResolver
17
 */
18
trait ApiLoaderTrait
19
{
20
    use LoaderTrait, LazyLoaderTrait;
21
22
    /**
23
     * @var ApiClientInterface
24
     */
25
    protected $restApiClient;
26
27
    /**
28
     * @var SerializerInterface
29
     */
30
    protected $serializer;
31
32
    /**
33
     * Construct.
34
     *
35
     * @param ApiClientInterface  $restApiClient
36
     * @param SerializerInterface $serializer
37
     */
38
    public function __construct(
39
        ApiClientInterface $restApiClient,
40
        SerializerInterface $serializer
41
    ) {
42
        $this->restApiClient = $restApiClient;
43
        $this->serializer = $serializer;
44
    }
45
46
    /**
47
     * Resolve given filters.
48
     *
49
     * @param array $filters
50
     *
51
     * @return array
52
     */
53
    private function resolveFilters(array $filters = array())
54
    {
55
        $this->filterResolver->setDefined('scope');
56
57
        return $this->filterResolver->resolve($filters);
58
    }
59
60
    /**
61
     * Performs an Api call on given method.
62
     *
63
     * @param string   $method
64
     * @param array    $query
65
     * @param callable $onSuccess
66
     * @param mixed    $emptyValue
67
     */
68
    private function apiCall(
69
        $method,
70
        array $query = array(),
71
        callable $onSuccess,
72
        $emptyValue = null
73
    ) {
74
        try {
75
            return $onSuccess(
76
                $this->restApiClient->$method($query)
77
            );
78
        } catch (BadResponseException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\BadResponseException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
79
            if (($response = $e->getResponse())
80
                && $response->getStatusCode() == 404
81
            ) {
82
                return $emptyValue;
83
            }
84
85
            throw $e;
86
        }
87
    }
88
89
    /**
90
     * @see LoaderInterface::retrieveAll()
91
     */
92
    public function retrieveAll(array $filters = array(), $limit = null, $offset = null)
0 ignored issues
show
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        return $this->apiCall(
95
            'cget',
96
            $this->resolveFilters($filters),
97
            function (Response $response) {
98
                return $this->serializer
99
                    ->deserialize(
100
                        (string) $response->getBody(),
101
                        $this->collectionClass,
102
                        'json'
103
                    )
104
                    ->map(function ($entity) {
105
                        return $this->loadDelegates($entity);
106
                    })
107
                ;
108
            },
109
            new $this->collectionClass()
110
        );
111
    }
112
113
    /**
114
     * @see LoaderInterface::retrieveOne()
115
     */
116
    public function retrieveOne(array $filters = array())
117
    {
118
        return $this->retrieveAll($filters)->first() ?: null;
119
    }
120
121
    /**
122
     * @see LoaderInterface::retrieve()
123
     */
124
    public function retrieve($id)
125
    {
126
        return $this->apiCall('get', array('id' => $id), function (Response $response) {
127
            return $this->loadDelegates(
128
                $this->serializer->deserialize(
129
                    (string) $response->getBody(),
130
                    $this->entityClass,
131
                    'json'
132
                )
133
            );
134
        });
135
    }
136
}
137