Completed
Branch next (92e3a0)
by Neomerx
01:44
created

RelationshipDataIsResource::getResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 2
nc 2
nop 0
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Parser\RelationshipData;
4
5
/**
6
 * Copyright 2015-2019 [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\Parser\IdentifierInterface;
23
use Neomerx\JsonApi\Contracts\Parser\PositionInterface;
24
use Neomerx\JsonApi\Contracts\Parser\RelationshipDataInterface;
25
use Neomerx\JsonApi\Contracts\Parser\ResourceInterface;
26
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
27
use Neomerx\JsonApi\Exceptions\LogicException;
28
use function Neomerx\JsonApi\I18n\format as _;
29
30
/**
31
 * @package Neomerx\JsonApi
32
 */
33
class RelationshipDataIsResource extends BaseRelationshipData implements RelationshipDataInterface
34
{
35
    /** @var string */
36
    public const MSG_INVALID_OPERATION = 'Invalid operation.';
37
38
    /**
39
     * @var mixed
40
     */
41
    private $resource;
42
43
    /**
44
     * @var null|ResourceInterface
45
     */
46
    private $parsedResource = null;
47
48
    /**
49
     * @param FactoryInterface          $factory
50
     * @param SchemaContainerInterface  $schemaContainer
51
     * @param PositionInterface         $position
52
     * @param mixed                     $resource
53
     */
54 23
    public function __construct(
55
        FactoryInterface $factory,
56
        SchemaContainerInterface $schemaContainer,
57
        PositionInterface $position,
58
        $resource
59
    ) {
60 23
        parent::__construct($factory, $schemaContainer, $position);
61
62 23
        $this->resource = $resource;
63 23
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function isCollection(): bool
69
    {
70
        return false;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function isNull(): bool
77
    {
78
        return false;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 22
    public function isResource(): bool
85
    {
86 22
        return true;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function isIdentifier(): bool
93
    {
94
        return false;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function getIdentifier(): IdentifierInterface
101
    {
102
        throw new LogicException(_(static::MSG_INVALID_OPERATION));
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function getIdentifiers(): iterable
109
    {
110
        throw new LogicException(_(static::MSG_INVALID_OPERATION));
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 22
    public function getResource(): ResourceInterface
117
    {
118 22
        if ($this->parsedResource === null) {
119 22
            $this->parsedResource = $this->createParsedResource($this->resource);
120
        }
121
122 22
        return $this->parsedResource;
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function getResources(): iterable
129
    {
130
        throw new LogicException(_(static::MSG_INVALID_OPERATION));
131
    }
132
}
133