Completed
Push — master ( 4455f5...4b418f )
by Joschi
07:33
created

Module::autorun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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