Completed
Push — master ( 519f76...318dd5 )
by Raffael
24:13 queued 15:29
created

OdataRest::count()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Endpoint;
13
14
use Generator;
15
use GuzzleHttp\Client;
16
use GuzzleHttp\Exception\RequestException;
17
use Psr\Log\LoggerInterface;
18
use Tubee\Collection\CollectionInterface;
19
use Tubee\Endpoint\OdataRest\QueryTransformer;
20
use Tubee\EndpointObject\EndpointObjectInterface;
21
use Tubee\Workflow\Factory as WorkflowFactory;
22
23
class OdataRest extends AbstractRest
24
{
25
    /**
26
     * Kind.
27
     */
28
    public const KIND = 'OdataRestEndpoint';
29
30
    /**
31
     * Init endpoint.
32
     */
33
    public function __construct(string $name, string $type, Client $client, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, array $resource = [])
34
    {
35
        $this->container = 'value';
36
        parent::__construct($name, $type, $client, $collection, $workflow, $logger, $resource);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function transformQuery(?array $query = null)
43
    {
44
        if ($this->filter_all !== null) {
45
            return QueryTransformer::transform($this->getFilterAll());
46
        }
47
        if (!empty($query)) {
48
            if ($this->filter_all === null) {
49
                return QueryTransformer::transform($query);
50
            }
51
52
            return QueryTransformer::transform([
53
                    '$and' => [
54
                        $this->getFilterAll(),
55
                        $query,
56
                    ],
57
                ]);
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function count(?array $query = null): int
67
    {
68
        $options = $this->getRequestOptions();
69
        $query = $this->transformQuery($query);
70
71
        if ($query !== null) {
72
            $options['query']['$filter'] = $query;
73
        }
74
75
        $options['query']['$count'] = true;
76
        $response = $this->client->get('', $options);
77
78
        return (int) $this->decodeResponse($response);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getAll(?array $query = null): Generator
85
    {
86
        $options = $this->getRequestOptions();
87
        $query = $this->transformQuery($query);
88
        $this->logGetAll($query);
89
90
        if ($query !== null) {
91
            $options['query']['$filter'] = $query;
92
        }
93
94
        $i = 0;
95
        $response = $this->client->get('', $options);
96
        $data = $this->getResponse($response);
97
98
        foreach ($data as $object) {
99
            yield $this->build($object);
100
        }
101
102
        return $i;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getOne(array $object, ?array $attributes = []): EndpointObjectInterface
109
    {
110
        $filter = $this->transformQuery($this->getFilterOne($object));
111
        $this->logGetOne($filter);
112
113
        $options = $this->getRequestOptions();
114
        $options['query']['$filter'] = $filter;
115
        $attributes[] = $this->identifier;
116
        $options['query']['$select'] = join(',', $attributes);
117
118
        try {
119
            $result = $this->client->get('', $options);
120
            $data = $this->getResponse($result);
121
        } catch (RequestException $e) {
122
            if ($e->getCode() === 404) {
123
                throw new Exception\ObjectNotFound('no object found with filter '.$filter);
124
            }
125
126
            throw $e;
127
        }
128
129
        if (count($data) > 1) {
130
            throw new Exception\ObjectMultipleFound('found more than one object with filter '.$filter);
131
        }
132
        if (count($data) === 0) {
133
            throw new Exception\ObjectNotFound('no object found with filter '.$filter);
134
        }
135
136
        return $this->build(array_shift($data), $filter);
137
    }
138
}
139