Completed
Push — develop ( dcfc04...3d10a6 )
by Neomerx
04:33
created

Factory::createEncoder()   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 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php namespace Limoncello\Flute;
2
3
/**
4
 * Copyright 2015-2018 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Doctrine\DBAL\Connection;
20
use Limoncello\Container\Traits\HasContainerTrait;
21
use Limoncello\Contracts\Data\ModelSchemaInfoInterface;
22
use Limoncello\Flute\Adapters\ModelQueryBuilder;
23
use Limoncello\Flute\Contracts\Api\CrudInterface;
24
use Limoncello\Flute\Contracts\Encoder\EncoderInterface;
25
use Limoncello\Flute\Contracts\FactoryInterface;
26
use Limoncello\Flute\Contracts\Models\ModelStorageInterface;
27
use Limoncello\Flute\Contracts\Models\PaginatedDataInterface;
28
use Limoncello\Flute\Contracts\Models\TagStorageInterface;
29
use Limoncello\Flute\Contracts\Schema\JsonSchemasInterface;
30
use Limoncello\Flute\Encoder\Encoder;
31
use Limoncello\Flute\Models\ModelStorage;
32
use Limoncello\Flute\Models\PaginatedData;
33
use Limoncello\Flute\Models\TagStorage;
34
use Limoncello\Flute\Schema\JsonSchemas;
35
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface as JsonApiFactoryInterface;
36
use Neomerx\JsonApi\Factories\Factory as JsonApiFactory;
37
use Neomerx\JsonApi\Schema\ErrorCollection;
38
use Psr\Container\ContainerInterface;
39
40
/**
41
 * @package Limoncello\Flute
42
 *
43
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
44
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
45
 */
46
class Factory implements FactoryInterface
47
{
48
    use HasContainerTrait;
49
50
    /**
51
     * @var JsonApiFactoryInterface
52
     */
53
    private $jsonApiFactory = null;
54
55
    /**
56
     * @param ContainerInterface $container
57
     */
58 95
    public function __construct(ContainerInterface $container)
59
    {
60 95
        $this->setContainer($container);
61
    }
62
63
    /**
64
     * @return JsonApiFactoryInterface
65
     */
66 55
    public function getJsonApiFactory()
67
    {
68 55
        if ($this->jsonApiFactory === null) {
69 55
            $this->jsonApiFactory = new JsonApiFactory();
70
        }
71
72 55
        return $this->jsonApiFactory;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 29
    public function createModelQueryBuilder(
79
        Connection $connection,
80
        string $modelClass,
81
        ModelSchemaInfoInterface $modelSchemas
82
    ): ModelQueryBuilder {
83 29
        return new ModelQueryBuilder($connection, $modelClass, $modelSchemas);
84
    }
85
86
    /**
87
     * @param mixed $data
88
     *
89
     * @return PaginatedDataInterface
90
     */
91 16
    public function createPaginatedData($data): PaginatedDataInterface
92
    {
93 16
        return new PaginatedData($data);
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function createErrorCollection(): ErrorCollection
100
    {
101
        return new ErrorCollection();
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 7
    public function createModelStorage(ModelSchemaInfoInterface $modelSchemas): ModelStorageInterface
108
    {
109 7
        return new ModelStorage($modelSchemas);
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 7
    public function createTagStorage(): TagStorageInterface
116
    {
117 7
        return new TagStorage();
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 55
    public function createJsonSchemas(
124
        array $modelToSchemaMap,
125
        array $typeToSchemaMap,
126
        ModelSchemaInfoInterface $modelSchemas
127
    ): JsonSchemasInterface {
128 55
        return new JsonSchemas($this->getJsonApiFactory(), $modelToSchemaMap, $typeToSchemaMap, $modelSchemas);
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    public function createEncoder(JsonSchemasInterface $schemas): EncoderInterface
135
    {
136
        return new Encoder($this->getJsonApiFactory(), $schemas);
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142 1
    public function createApi(string $apiClass): CrudInterface
143
    {
144 1
        $api = new $apiClass($this->getContainer());
145
146 1
        return $api;
147
    }
148
}
149