Completed
Push — master ( 3bf76e...9f9c26 )
by Joschi
04:22
created

Module::configureDependencyInjection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 30
cc 1
eloc 16
nc 1
nop 1
ccs 17
cts 17
cp 1
crap 1
rs 8.8571
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\CommonMarkPayloadProcessorInterface;
42
use Apparat\Object\Application\Model\Object\AbstractCommonMarkObject;
43
use Apparat\Object\Application\Model\Object\Manager;
44
use Apparat\Object\Domain\Model\Object\ManagerInterface;
45
use Apparat\Object\Domain\Repository\AdapterStrategyFactoryInterface;
46
use Apparat\Object\Domain\Repository\AutoConnectorInterface;
47
use Apparat\Object\Domain\Repository\Service;
48
use Apparat\Object\Infrastructure\Factory\AdapterStrategyFactory;
49
use Apparat\Object\Infrastructure\Repository\AutoConnector;
50
use Apparat\Object\Infrastructure\Utilities\CommonMarkPayloadProcessor;
51
use Dotenv\Dotenv;
52
53
/**
54
 * Object module
55
 *
56
 * @package Apparat\Object
57
 * @subpackage Apparat\Object
58
 */
59
class Module extends AbstractModule
60
{
61
    /**
62
     * Module name
63
     *
64
     * @var string
65
     */
66
    const NAME = 'object';
67
68
    /*******************************************************************************
69
     * PUBLIC METHODS
70
     *******************************************************************************/
71
72
    /**
73
     * Validate the environment
74
     *
75
     * @param Dotenv $environment Environment
76
     */
77 1
    protected static function validateEnvironment(Dotenv $environment)
78
    {
79 1
        parent::validateEnvironment($environment);
80
81
        // Validate the required environment variables
82 1
        $environment->required('APPARAT_BASE_URL')->notEmpty();
83 1
        $environment->required('OBJECT_RESOURCE_EXTENSION')->notEmpty();
84 1
        $environment->required('OBJECT_DATE_PRECISION')->isInteger()->allowedValues([0, 1, 2, 3, 4, 5, 6]);
85
86
        // In-depth validation of the apparat base URL
87 1
        $apparatBaseUrl = getenv('APPARAT_BASE_URL');
88 1
        self::isAbsoluteBareUrl($apparatBaseUrl);
89
90
        // Normalize the apparat base URL
91 1
        putenv('APPARAT_BASE_URL='.rtrim($apparatBaseUrl, '/').'/');
92 1
    }
93
94
    /**
95
     * Test whether a URL is absolute and doesn't have query parameters and / or a fragment
96
     *
97
     * @param string $url URL
98
     * @return boolean If the URL is absolute and has neither query parameters or a fragment
99
     * @throws \RuntimeException If the URL is not absolute / valid
100
     * @throws \RuntimeException If the URL has query parameters
101
     * @throws \RuntimeException If the URL has a fragment
102
     */
103 40
    public static function isAbsoluteBareUrl($url)
104
    {
105 40
        if (!filter_var($url) || !preg_match("%^https?\:\/\/%i", $url)) {
106 1
            throw new \RuntimeException(sprintf('Apparat base URL "%s" must be valid', $url), 1451776352);
107
        }
108 39
        if (strlen(parse_url($url, PHP_URL_QUERY))) {
109 3
            throw new \RuntimeException(
110 3
                sprintf('Apparat base URL "%s" must not contain query parameters', $url),
111
                1451776509
112 3
            );
113
        }
114 37
        if (strlen(parse_url($url, PHP_URL_FRAGMENT))) {
115 1
            throw new \RuntimeException(sprintf('Apparat base URL "%s" must not contain a fragment', $url), 1451776570);
116
        }
117
118 36
        return true;
119
    }
120
121
    /*******************************************************************************
122
     * PRIVATE METHODS
123
     *******************************************************************************/
124
125
    /**
126
     * Configure the dependency injection container
127
     *
128
     * @param DependencyInjectionContainerInterface $diContainer Dependency injection container
129
     * @return void
130
     */
131 1
    public function configureDependencyInjection(DependencyInjectionContainerInterface $diContainer)
132
    {
133 1
        parent::configureDependencyInjection($diContainer);
134
135
        // Configure the repository service
136 1
        $diContainer->register(Service::class, [
137 1
            'shared' => true,
138
            'substitutions' => [
139
                AutoConnectorInterface::class => [
140 1
                    'instance' => AutoConnector::class,
141 1
                ],
142
                AdapterStrategyFactoryInterface::class => [
143 1
                    'instance' => AdapterStrategyFactory::class,
144 1
                ],
145
                ManagerInterface::class => [
146 1
                    'instance' => Manager::class,
147 1
                ],
148
            ]
149 1
        ]);
150
151
        // Configure the CommonMark payload processor
152 1
        $diContainer->register(AbstractCommonMarkObject::class, [
153 1
            'shared' => true,
154
            'substitutions' => [
155
                CommonMarkPayloadProcessorInterface::class => [
156 1
                    'instance' => CommonMarkPayloadProcessor::class,
157 1
                ],
158
            ]
159 1
        ]);
160 1
    }
161
}
162