Completed
Push — develop ( 29d040...599906 )
by Neomerx
06:08 queued 04:31
created

Factory::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
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\ModelSchemeInfoInterface;
22
use Limoncello\Contracts\L10n\FormatterInterface;
23
use Limoncello\Flute\Adapters\Repository;
24
use Limoncello\Flute\Api\ModelsData;
25
use Limoncello\Flute\Contracts\Adapters\FilterOperationsInterface;
26
use Limoncello\Flute\Contracts\Adapters\RepositoryInterface;
27
use Limoncello\Flute\Contracts\Api\ModelsDataInterface;
28
use Limoncello\Flute\Contracts\Encoder\EncoderInterface;
29
use Limoncello\Flute\Contracts\FactoryInterface;
30
use Limoncello\Flute\Contracts\Models\ModelStorageInterface;
31
use Limoncello\Flute\Contracts\Models\PaginatedDataInterface;
32
use Limoncello\Flute\Contracts\Models\RelationshipStorageInterface;
33
use Limoncello\Flute\Contracts\Models\TagStorageInterface;
34
use Limoncello\Flute\Contracts\Schema\JsonSchemesInterface;
35
use Limoncello\Flute\Encoder\Encoder;
36
use Limoncello\Flute\Models\ModelStorage;
37
use Limoncello\Flute\Models\PaginatedData;
38
use Limoncello\Flute\Models\RelationshipStorage;
39
use Limoncello\Flute\Models\TagStorage;
40
use Limoncello\Flute\Schema\JsonSchemes;
41
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface as JsonApiFactoryInterface;
42
use Neomerx\JsonApi\Encoder\EncoderOptions;
43
use Neomerx\JsonApi\Exceptions\ErrorCollection;
44
use Neomerx\JsonApi\Factories\Factory as JsonApiFactory;
45
use Psr\Container\ContainerInterface;
46
47
/**
48
 * @package Limoncello\Flute
49
 *
50
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
51
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
52
 */
53
class Factory implements FactoryInterface
54
{
55
    use HasContainerTrait;
56
57
    /**
58
     * @var JsonApiFactoryInterface
59
     */
60
    private $jsonApiFactory = null;
61
62
    /**
63
     * @param ContainerInterface $container
64
     */
65
    public function __construct(ContainerInterface $container)
66 58
    {
67
        $this->setContainer($container);
68 58
    }
69
70
    /**
71
     * @return JsonApiFactoryInterface
72
     */
73
    public function getJsonApiFactory()
74 38
    {
75
        if ($this->jsonApiFactory === null) {
76 38
            $this->jsonApiFactory = new JsonApiFactory();
77 38
        }
78
79
        return $this->jsonApiFactory;
80 38
    }
81
82
    /**
83
     * @param mixed $data
84
     *
85
     * @return PaginatedDataInterface
86
     */
87
    public function createPaginatedData($data): PaginatedDataInterface
88 31
    {
89
        return new PaginatedData($data);
90 31
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function createErrorCollection(): ErrorCollection
96 36
    {
97
        return new ErrorCollection();
98 36
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function createRelationshipStorage(): RelationshipStorageInterface
104 8
    {
105
        return new RelationshipStorage($this);
106 8
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function createModelStorage(ModelSchemeInfoInterface $modelSchemes): ModelStorageInterface
112 8
    {
113
        return new ModelStorage($modelSchemes);
114 8
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function createTagStorage(): TagStorageInterface
120 8
    {
121
        return new TagStorage();
122 8
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function createModelsData(
128 25
        PaginatedDataInterface $paginatedData,
129
        RelationshipStorageInterface $relationshipStorage = null
130
    ): ModelsDataInterface {
131
        return new ModelsData($paginatedData, $relationshipStorage);
132 25
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137
    public function createRepository(
138 44
        Connection $connection,
139
        ModelSchemeInfoInterface $modelSchemes,
140
        FilterOperationsInterface $filterOperations,
141
        FormatterInterface $fluteMsgFormatter
142
    ): RepositoryInterface {
143
        return new Repository($connection, $modelSchemes, $filterOperations, $fluteMsgFormatter);
144 44
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public function createJsonSchemes(array $schemes, ModelSchemeInfoInterface $modelSchemes): JsonSchemesInterface
150 38
    {
151
        return new JsonSchemes($this->getJsonApiFactory(), $schemes, $modelSchemes);
152 38
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157
    public function createEncoder(
158 21
        JsonSchemesInterface $schemes,
159
        EncoderOptions $encoderOptions = null
160
    ): EncoderInterface {
161
        return new Encoder($this->getJsonApiFactory(), $schemes, $encoderOptions);
162 21
    }
163
164
    /**
165
     * @inheritdoc
166
     */
167
    public function createApi(string $apiClass)
168 20
    {
169
        $api = new $apiClass($this->getContainer());
170 20
171
        return $api;
172 20
    }
173
}
174