Completed
Push — master ( abe227...316baf )
by Raffael
13:55 queued 08:01
created

DataObject::getRelations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee.io
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;
13
14
use Generator;
15
use MongoDB\BSON\ObjectIdInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Tubee\Collection\CollectionInterface;
18
use Tubee\DataObject\DataObjectInterface;
19
use Tubee\DataObjectRelation\Factory as DataObjectRelationFactory;
20
use Tubee\Resource\AbstractResource;
21
use Tubee\Resource\AttributeResolver;
22
23
class DataObject extends AbstractResource implements DataObjectInterface
24
{
25
    /**
26
     * Datatype.
27
     *
28
     * @var CollectionInterface
29
     */
30
    protected $collection;
31
32
    /**
33
     * Data object relation factory.
34
     *
35
     * @var DataObjectRelationFactory
36
     */
37
    protected $relation_factory;
38
39
    /**
40
     * Data object.
41
     */
42
    public function __construct(array $resource, CollectionInterface $collection, DataObjectRelationFactory $relation_factory)
43
    {
44
        $this->resource = $resource;
45
        $this->collection = $collection;
46
        $this->relation_factory = $relation_factory;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function decorate(ServerRequestInterface $request): array
53
    {
54
        $collection = $this->collection->getName();
55
        $namespace = $this->collection->getResourceNamespace()->getName();
56
57
        $resource = [
58
            '_links' => [
59
                'namespace' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$namespace)],
60
                'collection' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$namespace.'/collections/'.$collection)],
61
            ],
62
            'kind' => 'DataObject',
63
            'namespace' => $namespace,
64
            'collection' => $collection,
65
            'data' => $this->getData(),
66
            'status' => function ($object) {
67
                $endpoints = $object->getEndpoints();
68
                foreach ($endpoints as &$endpoint) {
69
                    $endpoint['last_sync'] = $endpoint['last_sync']->toDateTime()->format('c');
70
                    $endpoint['garbage'] = isset($endpoint['garbage']) ? $endpoint['garbage'] : false;
71
                    $endpoint['result'] = isset($endpoint['result']) ? $endpoint['result'] : null;
72
                }
73
74
                return ['endpoints' => $endpoints];
75
            },
76
        ];
77
78
        return AttributeResolver::resolve($request, $this, $resource);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getHistory(?array $query = null, ?int $offset = null, ?int $limit = null): Iterable
85
    {
86
        return $this->collection->getObjectHistory($this->getId(), $query, $offset, $limit);
0 ignored issues
show
Bug introduced by
The method getObjectHistory() does not exist on Tubee\Collection\CollectionInterface. Did you maybe mean getObject()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getData(): array
93
    {
94
        return $this->resource['data'];
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getCollection(): CollectionInterface
101
    {
102
        return $this->collection;
103
    }
104
105
    /**
106
     * Get endpoints.
107
     */
108
    public function getEndpoints(): array
109
    {
110
        if (!isset($this->resource['endpoints'])) {
111
            return [];
112
        }
113
114
        return $this->resource['endpoints'];
115
    }
116
117
    /**
118
     * Add relation.
119
     */
120
    public function createOrUpdateRelation(DataObjectInterface $object, array $context = [], bool $simulate = false, ?array $endpoints = null): ObjectIdInterface
121
    {
122
        return $this->relation_factory->createOrUpdate($this, $object, $context, $simulate, $endpoints);
0 ignored issues
show
Documentation introduced by
$this is of type this<Tubee\DataObject>, but the function expects a object<Tubee\ResourceNam...urceNamespaceInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$context is of type array, but the function expects a object<Tubee\DataObject\DataObjectInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$simulate is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
    }
124
125
    /**
126
     * Get relatives.
127
     */
128
    public function getRelations(): Generator
129
    {
130
        return $this->relation_factory->getAllFromObject($this);
131
    }
132
133
    /**
134
     * Get relative.
135
     */
136
    public function getRelation(string $name): Generator
137
    {
138
        return $this->relation_factory->getOneFromObject($this, $name);
139
    }
140
}
141