Completed
Push — develop ( 46135c...90e0de )
by Neomerx
03:10
created

Factory::createEncoder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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