BaseRelationshipData::createParsedIdentifier()   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
dl 0
loc 4
c 0
b 0
f 0
cc 1
nc 1
nop 1
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Parser\RelationshipData;
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\Parser\EditableContextInterface;
23
use Neomerx\JsonApi\Contracts\Parser\IdentifierInterface as ParserIdentifierInterface;
24
use Neomerx\JsonApi\Contracts\Parser\RelationshipDataInterface;
25
use Neomerx\JsonApi\Contracts\Parser\ResourceInterface;
26
use Neomerx\JsonApi\Contracts\Schema\IdentifierInterface as SchemaIdentifierInterface;
27
use Neomerx\JsonApi\Contracts\Schema\PositionInterface;
28
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
29
30
/**
31
 * @package Neomerx\JsonApi
32
 */
33
abstract class BaseRelationshipData implements RelationshipDataInterface
34
{
35
    /**
36
     * @var FactoryInterface
37
     */
38
    private $factory;
39
40
    /**
41
     * @var SchemaContainerInterface
42
     */
43
    private $schemaContainer;
44
45
    /**
46
     * @var EditableContextInterface
47
     */
48
    private $context;
49
50
    /**
51
     * @var PositionInterface
52
     */
53
    private $position;
54
55
    /**
56
     * @param FactoryInterface         $factory
57
     * @param SchemaContainerInterface $schemaContainer
58
     * @param EditableContextInterface $context
59
     * @param PositionInterface        $position
60
     */
61 42
    public function __construct(
62
        FactoryInterface $factory,
63
        SchemaContainerInterface $schemaContainer,
64
        EditableContextInterface $context,
65
        PositionInterface $position
66
    ) {
67 42
        $this->factory         = $factory;
68 42
        $this->schemaContainer = $schemaContainer;
69 42
        $this->context         = $context;
70 42
        $this->position        = $position;
71 42
    }
72
73
    /**
74
     * @param mixed $resource
75
     *
76
     * @return ResourceInterface
77
     */
78 33
    protected function createParsedResource($resource): ResourceInterface
79
    {
80 33
        \assert(
81 33
            $this->schemaContainer->hasSchema($resource),
82 33
            'No Schema found for resource `' . \get_class($resource) . '`.'
83
        );
84
85 33
        return $this->factory->createParsedResource(
86 33
            $this->context,
87 33
            $this->position,
88 33
            $this->schemaContainer,
89 33
            $resource
90
        );
91
    }
92
93
    /**
94
     * @param SchemaIdentifierInterface $identifier
95
     *
96
     * @return ResourceInterface
97
     */
98 2
    protected function createParsedIdentifier(SchemaIdentifierInterface $identifier): ParserIdentifierInterface
99
    {
100 2
        return $this->factory->createParsedIdentifier($this->position, $identifier);
101
    }
102
}
103