BaseSchema::hasResourceMeta()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Schema;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
22
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface;
23
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
24
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface;
25
use Neomerx\JsonApi\Exceptions\LogicException;
26
27
/**
28
 * @package Neomerx\JsonApi
29
 */
30
abstract class BaseSchema implements SchemaInterface
31
{
32
    /**
33
     * @var FactoryInterface
34
     */
35
    private $factory;
36
37
    /**
38
     * @var null|string
39
     */
40
    private $subUrl = null;
41
42
    /**
43
     * @param FactoryInterface $factory
44
     */
45 70
    public function __construct(FactoryInterface $factory)
46
    {
47 70
        $this->factory = $factory;
48 70
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 60
    public function getSelfLink($resource): LinkInterface
54
    {
55 60
        return $this->factory->createLink(true, $this->getSelfSubUrl($resource), false);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 60
    public function getLinks($resource): iterable
62
    {
63
        $links = [
64 60
            LinkInterface::SELF => $this->getSelfLink($resource),
65
        ];
66
67 60
        return $links;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 42 View Code Duplication
    public function getRelationshipSelfLink($resource, string $name): LinkInterface
74
    {
75
        // Feel free to override this method to change default URL or add meta
76
77 42
        $url = $this->getSelfSubUrl($resource) . '/' . DocumentInterface::KEYWORD_RELATIONSHIPS . '/' . $name;
78
79 42
        return $this->factory->createLink(true, $url, false);
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 16 View Code Duplication
    public function getRelationshipRelatedLink($resource, string $name): LinkInterface
86
    {
87
        // Feel free to override this method to change default URL or add meta
88
89 16
        $url = $this->getSelfSubUrl($resource) . '/' . $name;
90
91 16
        return $this->factory->createLink(true, $url, false);
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 35
    public function hasIdentifierMeta($resource): bool
98
    {
99 35
        return false;
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 1
    public function getIdentifierMeta($resource)
106
    {
107
        // default schema does not provide any meta
108 1
        throw new LogicException();
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114 62
    public function hasResourceMeta($resource): bool
115
    {
116 62
        return false;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122 1
    public function getResourceMeta($resource)
123
    {
124
        // default schema does not provide any meta
125 1
        throw new LogicException();
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131 23
    public function isAddSelfLinkInRelationshipByDefault(string $relationshipName): bool
132
    {
133 23
        return true;
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139 19
    public function isAddRelatedLinkInRelationshipByDefault(string $relationshipName): bool
140
    {
141 19
        return true;
142
    }
143
144
    /**
145
     * @return FactoryInterface
146
     */
147 35
    protected function getFactory(): FactoryInterface
148
    {
149 35
        return $this->factory;
150
    }
151
152
    /**
153
     * Get resources sub-URL.
154
     *
155
     * @return string
156
     */
157 67
    protected function getResourcesSubUrl(): string
158
    {
159 67
        if ($this->subUrl === null) {
160 67
            $this->subUrl = '/' . $this->getType();
161
        }
162
163 67
        return $this->subUrl;
164
    }
165
166
    /**
167
     * @param mixed $resource
168
     *
169
     * @return string
170
     */
171 67
    protected function getSelfSubUrl($resource): string
172
    {
173 67
        return $this->getResourcesSubUrl() . '/' . $this->getId($resource);
174
    }
175
}
176