Completed
Push — master ( e50616...a5acb0 )
by Raffael
28:40 queued 24:37
created

DataObject::deleteRelation()   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 2
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;
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 toArray(): array
93
    {
94
        $resource = $this->resource;
95
        $resource['namespace'] = $this->collection->getResourceNamespace()->getName();
96
        $resource['collection'] = $this->collection->getName();
97
98
        return $resource;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getCollection(): CollectionInterface
105
    {
106
        return $this->collection;
107
    }
108
109
    /**
110
     * Get endpoints.
111
     */
112
    public function getEndpoints(): array
113
    {
114
        if (!isset($this->resource['endpoints'])) {
115
            return [];
116
        }
117
118
        return $this->resource['endpoints'];
119
    }
120
121
    /**
122
     * Add or update relation.
123
     */
124
    public function createOrUpdateRelation(DataObjectInterface $object, array $context = [], bool $simulate = false, ?array $endpoints = null): ObjectIdInterface
125
    {
126
        return $this->relation_factory->createOrUpdate($this, $object, $context, $simulate, $endpoints);
127
    }
128
129
    /**
130
     * Delete relation.
131
     */
132
    public function deleteRelation(DataObjectInterface $object, bool $simulate = false): bool
133
    {
134
        return $this->relation_factory->deleteFromObject($this, $object, $simulate);
135
    }
136
137
    /**
138
     * Get relatives.
139
     */
140
    public function getRelations(): Generator
141
    {
142
        return $this->relation_factory->getAllFromObject($this);
143
    }
144
145
    /**
146
     * Get relative.
147
     */
148
    public function getRelation(string $name): Generator
149
    {
150
        return $this->relation_factory->getOneFromObject($this, $name);
151
    }
152
}
153