Factory::createModelStorage()   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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare (strict_types = 1);
2
3
namespace Limoncello\Flute;
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 Doctrine\DBAL\Connection;
22
use Limoncello\Container\Traits\HasContainerTrait;
23
use Limoncello\Contracts\Data\ModelSchemaInfoInterface;
24
use Limoncello\Flute\Adapters\ModelQueryBuilder;
25
use Limoncello\Flute\Contracts\Api\CrudInterface;
26
use Limoncello\Flute\Contracts\Encoder\EncoderInterface;
27
use Limoncello\Flute\Contracts\FactoryInterface;
28
use Limoncello\Flute\Contracts\Models\ModelStorageInterface;
29
use Limoncello\Flute\Contracts\Models\PaginatedDataInterface;
30
use Limoncello\Flute\Contracts\Models\TagStorageInterface;
31
use Limoncello\Flute\Contracts\Schema\JsonSchemasInterface;
32
use Limoncello\Flute\Encoder\Encoder;
33
use Limoncello\Flute\Models\ModelStorage;
34
use Limoncello\Flute\Models\PaginatedData;
35
use Limoncello\Flute\Models\TagStorage;
36
use Limoncello\Flute\Schema\JsonSchemas;
37
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface as JsonApiFactoryInterface;
38
use Neomerx\JsonApi\Factories\Factory as JsonApiFactory;
39
use Neomerx\JsonApi\Schema\ErrorCollection;
40
use Psr\Container\ContainerInterface;
41
42
/**
43
 * @package Limoncello\Flute
44
 *
45
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
46
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
47
 */
48
class Factory implements FactoryInterface
49
{
50
    use HasContainerTrait;
51
52
    /**
53
     * @var JsonApiFactoryInterface
54
     */
55
    private $jsonApiFactory = null;
56
57
    /**
58
     * @param ContainerInterface $container
59
     */
60 91
    public function __construct(ContainerInterface $container)
61
    {
62 91
        $this->setContainer($container);
63
    }
64
65
    /**
66
     * @return JsonApiFactoryInterface
67
     */
68 58
    public function getJsonApiFactory()
69
    {
70 58
        if ($this->jsonApiFactory === null) {
71 58
            $this->jsonApiFactory = new JsonApiFactory();
72
        }
73
74 58
        return $this->jsonApiFactory;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 52
    public function createModelQueryBuilder(
81
        Connection $connection,
82
        string $modelClass,
83
        ModelSchemaInfoInterface $modelSchemas
84
    ): ModelQueryBuilder {
85 52
        return new ModelQueryBuilder($connection, $modelClass, $modelSchemas);
86
    }
87
88
    /**
89
     * @param mixed $data
90
     *
91
     * @return PaginatedDataInterface
92
     */
93 28
    public function createPaginatedData($data): PaginatedDataInterface
94
    {
95 28
        return new PaginatedData($data);
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 4
    public function createErrorCollection(): ErrorCollection
102
    {
103 4
        return new ErrorCollection();
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 21
    public function createModelStorage(ModelSchemaInfoInterface $modelSchemas): ModelStorageInterface
110
    {
111 21
        return new ModelStorage($modelSchemas);
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117 21
    public function createTagStorage(): TagStorageInterface
118
    {
119 21
        return new TagStorage();
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125 58
    public function createJsonSchemas(
126
        array $modelToSchemaMap,
127
        array $typeToSchemaMap,
128
        ModelSchemaInfoInterface $modelSchemas
129
    ): JsonSchemasInterface {
130 58
        return new JsonSchemas($this->getJsonApiFactory(), $modelToSchemaMap, $typeToSchemaMap, $modelSchemas);
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136 31
    public function createEncoder(JsonSchemasInterface $schemas): EncoderInterface
137
    {
138 31
        return new Encoder($this->getJsonApiFactory(), $schemas);
139
    }
140
141
    /**
142
     * @inheritdoc
143
     */
144 31
    public function createApi(string $apiClass): CrudInterface
145
    {
146 31
        $api = new $apiClass($this->getContainer());
147
148 31
        return $api;
149
    }
150
}
151