Completed
Push — master ( 88eef6...81252c )
by Joschi
02:44
created

Module::validateEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
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\Model\Object\Manager;
42
use Apparat\Object\Domain\Model\Object\ManagerInterface;
43
use Apparat\Object\Domain\Repository\AdapterStrategyFactoryInterface;
44
use Apparat\Object\Domain\Repository\AutoConnectorInterface;
45
use Apparat\Object\Domain\Repository\Service;
46
use Apparat\Object\Infrastructure\Factory\AdapterStrategyFactory;
47
use Apparat\Object\Infrastructure\Repository\AutoConnector;
48
use Dotenv\Dotenv;
49
50
/**
51
 * Object module
52
 *
53
 * @package Apparat\Object
54
 * @subpackage Apparat\Object
55
 */
56
class Module extends AbstractModule
57
{
58
    /**
59
     * Module name
60
     *
61
     * @var string
62
     */
63
    const NAME = 'object';
64
65
    /*******************************************************************************
66
     * PUBLIC METHODS
67
     *******************************************************************************/
68
69
    /**
70
     * Configure the dependency injection container
71
     *
72
     * @param DependencyInjectionContainerInterface $diContainer Dependency injection container
73
     * @return void
74
     */
75 1
    public function configureDependencyInjection(DependencyInjectionContainerInterface $diContainer)
76
    {
77 1
        parent::configureDependencyInjection($diContainer);
78
79
        // Configure the repository service
80 1
        $diContainer->register(Service::class, [
0 ignored issues
show
Bug introduced by
The method register() does not seem to exist on object<Apparat\Kernel\Po...tionContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81 1
            'shared' => true,
82
            'substitutions' => [
83
                AutoConnectorInterface::class => [
84 1
                    'instance' => AutoConnector::class,
85 1
                ],
86
                AdapterStrategyFactoryInterface::class => [
87 1
                    'instance' => AdapterStrategyFactory::class,
88 1
                ],
89
                ManagerInterface::class => [
90 1
                    'instance' => Manager::class,
91 1
                ],
92
            ]
93 1
        ]);
94 1
    }
95
96
    /**
97
     * Test whether a URL is absolute and doesn't have query parameters and / or a fragment
98
     *
99
     * @param string $url URL
100
     * @return boolean If the URL is absolut and has neither query parameters or a fragment
101
     * @throws \RuntimeException If the URL is not absolute / valid
102
     * @throws \RuntimeException If the URL has query parameters
103
     * @throws \RuntimeException If the URL has a fragment
104
     */
105 20
    public static function isAbsoluteBareUrl($url)
106
    {
107 20
        if (!filter_var($url) || !preg_match("%^https?\:\/\/%i", $url)) {
108 1
            throw new \RuntimeException(sprintf('Apparat base URL "%s" must be valid', $url), 1451776352);
109
        }
110 19
        if (strlen(parse_url($url, PHP_URL_QUERY))) {
111 3
            throw new \RuntimeException(
112 3
                sprintf('Apparat base URL "%s" must not contain query parameters', $url),
113
                1451776509
114 3
            );
115
        }
116 17
        if (strlen(parse_url($url, PHP_URL_FRAGMENT))) {
117 1
            throw new \RuntimeException(sprintf('Apparat base URL "%s" must not contain a fragment', $url), 1451776570);
118
        }
119
120 16
        return true;
121
    }
122
123
    /*******************************************************************************
124
     * PRIVATE METHODS
125
     *******************************************************************************/
126
127
    /**
128
     * Validate the environment
129
     *
130
     * @param Dotenv $environment Environment
131
     */
132 1
    protected static function validateEnvironment(Dotenv $environment)
133
    {
134 1
        parent::validateEnvironment($environment);
135
136
        // Validate the required environment variables
137 1
        $environment->required('APPARAT_BASE_URL')->notEmpty();
138 1
        $environment->required('OBJECT_RESOURCE_EXTENSION')->notEmpty();
139 1
        $environment->required('OBJECT_DATE_PRECISION')->isInteger()->allowedValues([0, 1, 2, 3, 4, 5, 6]);
140
141
        // In-depth validation of the apparat base URL
142 1
        $apparatBaseUrl = getenv('APPARAT_BASE_URL');
143 1
        self::isAbsoluteBareUrl($apparatBaseUrl);
144
145
        // Normalize the apparat base URL
146 1
        putenv('APPARAT_BASE_URL='.rtrim($apparatBaseUrl, '/').'/');
147 1
    }
148
}
149