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

Balloon::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 Psr\Log\LoggerInterface;
17
use Tubee\Collection\CollectionInterface;
18
use Tubee\EndpointObject\EndpointObjectInterface;
19
use Tubee\Workflow\Factory as WorkflowFactory;
20
21
class Balloon extends AbstractRest
22
{
23
    /**
24
     * Kind.
25
     */
26
    public const KIND = 'BalloonEndpoint';
27
28
    /**
29
     * Init endpoint.
30
     */
31 6
    public function __construct(string $name, string $type, Client $client, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, array $resource = [])
32
    {
33 6
        $this->container = 'data';
34 6
        parent::__construct($name, $type, $client, $collection, $workflow, $logger, $resource);
35 6
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 6
    public function transformQuery(?array $query = null)
41
    {
42 6
        if ($this->filter_all !== null && empty($query)) {
43
            return stripslashes($this->filter_all);
44
        }
45 6
        if (!empty($query)) {
46 5
            if ($this->filter_all === null) {
47 4
                return json_encode($query);
48
            }
49
50 1
            return '{"$and":['.stripslashes($this->filter_all).', '.json_encode($query).']}';
51
        }
52
53 1
        return null;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function count(?array $query = null): int
60
    {
61
        $query = $this->transformQuery($query);
62
63
        $options = $this->getRequestOptions();
64
        $options['query'] = [
65
            'query' => $query,
66
        ];
67
68
        $response = $this->client->get('', $options);
69
70
        return $this->decodeResponse($response)['total'] ?? 0;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getAll(?array $query = null): Generator
77
    {
78
        $query = $this->transformQuery($query);
79
        $this->logGetAll($query);
80
81
        $options = $this->getRequestOptions();
82
        $options['query'] = [
83
            'query' => $query,
84
        ];
85
86
        $i = 0;
87
        $response = $this->client->get('', $options);
88
        $data = $this->getResponse($response);
89
90
        foreach ($data as $object) {
91
            yield $this->build($object);
92
        }
93
94
        return $i;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 3
    public function getOne(array $object, ?array $attributes = []): EndpointObjectInterface
101
    {
102 3
        $filter = $this->transformQuery($this->getFilterOne($object));
103 3
        $this->logGetOne($filter);
104
105 3
        $options = $this->getRequestOptions();
106 3
        $options['query'] = [
107 3
            'query' => stripslashes($filter),
108
        ];
109
110 3
        $result = $this->client->get('', $options);
111 3
        $data = $this->getResponse($result);
112
113 3
        if (count($data) > 1) {
114 1
            throw new Exception\ObjectMultipleFound('found more than one object with filter '.$filter);
115
        }
116 2
        if (count($data) === 0) {
117 1
            throw new Exception\ObjectNotFound('no object found with filter '.$filter);
118
        }
119
120 1
        return $this->build(array_shift($data), $filter);
121
    }
122
}
123