Completed
Push — develop ( 6d3477...b4cdc6 )
by Neomerx
05:19
created

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