Module::configureDependencyInjection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 29
nc 1
nop 1
dl 0
loc 56
rs 9.7251
c 1
b 0
f 0
ccs 13
cts 13
cp 1
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object;
38
39
use Apparat\Kernel\Ports\AbstractModule;
40
use Apparat\Kernel\Ports\Contract\DependencyInjectionContainerInterface;
41
use Apparat\Object\Application\Contract\BinaryPayloadProcessorInterface;
42
use Apparat\Object\Application\Contract\CommonMarkPayloadProcessorInterface;
43
use Apparat\Object\Application\Model\Object\AbstractBinaryObject;
44
use Apparat\Object\Application\Model\Object\AbstractCommonMarkObject;
45
use Apparat\Object\Application\Model\Object\Manager;
46
use Apparat\Object\Application\Service\TypeService;
47
use Apparat\Object\Domain\Contract\TypeServiceInterface;
48
use Apparat\Object\Domain\Model\Object\ManagerInterface;
49
use Apparat\Object\Domain\Model\Object\Type;
50
use Apparat\Object\Domain\Model\Properties\MetaProperties;
51
use Apparat\Object\Domain\Repository\AdapterStrategyFactoryInterface;
52
use Apparat\Object\Domain\Repository\AutoConnectorInterface;
53
use Apparat\Object\Domain\Repository\Selector;
54
use Apparat\Object\Domain\Repository\Service;
55
use Apparat\Object\Infrastructure\Factory\AdapterStrategyFactory;
56
use Apparat\Object\Infrastructure\Repository\AutoConnector;
57
use Apparat\Object\Infrastructure\Utilities\BinaryPayloadProcessor;
58
use Apparat\Object\Infrastructure\Utilities\CommonMarkPayloadProcessor;
59
use Apparat\Object\Ports\Types\Object;
60
use Dotenv\Dotenv;
61
62
/**
63
 * Object module
64
 *
65
 * @package Apparat\Object
66
 * @subpackage Apparat\Object
67
 */
68
class Module extends AbstractModule
69
{
70
    /**
71
     * Module name
72
     *
73
     * @var string
74
     */
75
    const NAME = 'object';
76
77
    /**
78
     * Validate the environment
79
     *
80
     * @param Dotenv $environment Environment
81
     */
82 1
    protected static function validateEnvironment(Dotenv $environment)
83
    {
84 1
        parent::validateEnvironment($environment);
85
86
        // Validate the required environment variables
87 1
        $environment->required('APPARAT_BASE_URL')->notEmpty();
88 1
        $environment->required('OBJECT_RESOURCE_EXTENSION')->notEmpty();
89 1
        $environment->required('OBJECT_DATE_PRECISION')->isInteger()->allowedValues([0, 1, 2, 3, 4, 5, 6]);
90 1
        $environment->required('OBJECT_DEFAULT_PRIVACY')->notEmpty()->allowedValues(MetaProperties::$privacyLevels);
91 1
        $environment->required('OBJECT_DEFAULT_LANGUAGE')->notEmpty();
92 1
        $environment->required('OBJECT_ENABLE_TYPES')->notEmpty();
93
94
        // In-depth validation of the apparat base URL
95 1
        $apparatBaseUrl = getenv('APPARAT_BASE_URL');
96 1
        self::isAbsoluteBareUrl($apparatBaseUrl);
97
98
        // Normalize the apparat base URL
99 1
        putenv('APPARAT_BASE_URL='.rtrim($apparatBaseUrl, '/').'/');
100
101
        // Enable the configured object types
102 1
        array_map([Object::class, 'enableType'], explode(',', getenv('OBJECT_ENABLE_TYPES')));
103 1
    }
104
105
    /**
106
     * Test whether a URL is absolute and does not have query parameters and / or a fragment
107
     *
108
     * @param string $url URL
109
     * @return boolean If the URL is absolute and has neither query parameters or a fragment
110
     * @throws \RuntimeException If the URL is not absolute / valid
111
     * @throws \RuntimeException If the URL has query parameters
112
     * @throws \RuntimeException If the URL has a fragment
113
     */
114 71
    public static function isAbsoluteBareUrl($url)
115
    {
116 71
        if (!filter_var($url) || !preg_match("%^https?\:\/\/%i", $url)) {
117 1
            throw new \RuntimeException(sprintf('Apparat base URL "%s" must be valid', $url), 1451776352);
118
        }
119 70
        if (strlen(parse_url($url, PHP_URL_QUERY))) {
120 3
            throw new \RuntimeException(
121 3
                sprintf('Apparat base URL "%s" must not contain query parameters', $url),
122 3
                1451776509
123
            );
124
        }
125 68
        if (strlen(parse_url($url, PHP_URL_FRAGMENT))) {
126 1
            throw new \RuntimeException(sprintf('Apparat base URL "%s" must not contain a fragment', $url), 1451776570);
127
        }
128
129 67
        return true;
130
    }
131
132
    /**
133
     * Configure the dependency injection container
134
     *
135
     * @param DependencyInjectionContainerInterface $diContainer Dependency injection container
136
     * @return void
137
     */
138 1
    public function configureDependencyInjection(DependencyInjectionContainerInterface $diContainer)
139
    {
140 1
        parent::configureDependencyInjection($diContainer);
141
142
        // Configure the repository service
143 1
        $diContainer->register(Service::class, [
144 1
            'shared' => true,
145
            'substitutions' => [
146
                AutoConnectorInterface::class => [
147
                    'instance' => AutoConnector::class,
148
                ],
149
                AdapterStrategyFactoryInterface::class => [
150
                    'instance' => AdapterStrategyFactory::class,
151
                ],
152
                ManagerInterface::class => [
153
                    'instance' => Manager::class,
154
                ],
155
            ]
156
        ]);
157
158
        // Configure the type service
159 1
        $diContainer->register(Type::class, [
160
            'substitutions' => [
161
                TypeServiceInterface::class => [
162
                    'instance' => TypeService::class,
163
                ],
164
            ]
165 1
        ]);
166 1
        $diContainer->register(Selector::class, [
167
            'substitutions' => [
168
                TypeServiceInterface::class => [
169
                    'instance' => TypeService::class,
170
                ],
171
            ]
172 1
        ]);
173
174
        // Configure the CommonMark payload processor
175 1
        $diContainer->register(AbstractCommonMarkObject::class, [
176 1
            'shared' => false,
177
            'substitutions' => [
178
                CommonMarkPayloadProcessorInterface::class => [
179
                    'instance' => CommonMarkPayloadProcessor::class,
180
                ],
181
            ]
182
        ]);
183
184
        // Configure the binary payload processor
185 1
        $diContainer->register(AbstractBinaryObject::class, [
186 1
            'shared' => false,
187
            'substitutions' => [
188
                BinaryPayloadProcessorInterface::class => [
189
                    'instance' => BinaryPayloadProcessor::class,
190
                ],
191
            ]
192
        ]);
193 1
    }
194
}
195