Completed
Push — master ( faf2cb...9003f0 )
by Joschi
04:55
created

App::getVirtualHostService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * Toggl Dashboard
5
 *
6
 * @category    Tollwerk
7
 * @package     Tollwerk\Admin
8
 * @subpackage  Tollwerk\Admin\Ports
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 Tollwerk\Admin\Infrastructure;
38
39
use Doctrine\DBAL\Types\Type;
40
use Doctrine\ORM\EntityManager;
41
use Doctrine\ORM\Tools\Setup;
42
use Symfony\Component\Yaml\Yaml;
43
use Tollwerk\Admin\Application\Service\AccountService;
44
use Tollwerk\Admin\Application\Service\VirtualHostService;
45
use Tollwerk\Admin\Infrastructure\Doctrine\EnumVhosttypeType;
46
use Tollwerk\Admin\Infrastructure\Factory\PersistenceAdapterFactory;
47
use Tollwerk\Admin\Infrastructure\Strategy\DoctrineStorageAdapterStrategy;
48
49
/**
50
 * App
51
 *
52
 * @package Tollwerk\Admin
53
 * @subpackage Tollwerk\Admin\Ports
54
 */
55
class App
56
{
57
    /**
58
     * Configuration
59
     *
60
     * @var array
61
     */
62
    protected static $config;
63
    /**
64
     * Root directory
65
     *
66
     * @var string
67
     */
68
    protected static $rootDirectory;
69
    /**
70
     * Entity manager
71
     *
72
     * @var EntityManager
73
     */
74
    protected static $entityManager;
75
    /**
76
     * Developer mode
77
     *
78
     * @var boolean
79
     */
80
    protected static $devMode;
81
    /**
82
     * Account service
83
     *
84
     * @var AccountService
85
     */
86
    protected static $accountService = null;
87
    /**
88
     * Virtual host service
89
     *
90
     * @var VirtualHostService
91
     */
92
    protected static $vhostService = null;
93
    /**
94
     * App domain
95
     *
96
     * @var string
97
     */
98
    const DOMAIN = 'admin';
99
100
    /**
101
     * Bootstrap
102
     *
103
     * @see https://github.com/toggl/toggl_api_docs/blob/master/reports.md
104
     * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html
105
     *
106
     * @param bool $devMode Developer mode
107
     */
108
    public static function bootstrap($devMode = false)
109
    {
110
        self::$devMode = !!$devMode;
111
        self::$rootDirectory = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR;
112
113
        // Initialize the configuration
114
        $config = file_get_contents(self::$rootDirectory.'config'.DIRECTORY_SEPARATOR.'config.yml');
115
        self::$config = Yaml::parse($config);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symfony\Component\Yaml\Yaml::parse($config) can also be of type string or object<stdClass>. However, the property $config is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
116
117
        // Initialize doctrine
118
        self::initializeDoctrine();
119
    }
120
121
    /**
122
     * Initialize Doctrine
123
     */
124
    protected static function initializeDoctrine()
125
    {
126
        // If the Doctrine parameters don't exist
127
        if (empty(self::$config['doctrine'])
128
            || !is_array(self::$config['doctrine'])
129
            || empty(self::$config['doctrine']['dbparams'])
130
        ) {
131
            throw new \InvalidArgumentException('Invalid Doctrine database parameters', 1466175889);
132
        }
133
134
        $modelPaths = [
135
            self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin'
136
            .DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Model'
137
        ];
138
        $dbParams = self::$config['doctrine']['dbparams'];
139
        $config = Setup::createAnnotationMetadataConfiguration($modelPaths, self::$devMode);
140
141
        self::$entityManager = EntityManager::create($dbParams, $config);
142
        $platform = self::$entityManager->getConnection()->getDatabasePlatform();
143
        $platform->registerDoctrineTypeMapping('enum', 'string');
144
145
        // Register the virtual host type declaration
146
        Type::addType(EnumVhosttypeType::ENUM_TYPE, EnumVhosttypeType::class);
147
148
        // Set the locale
149
        $locale = self::getConfig('general.locale');
150
        putenv('LC_ALL='.$locale);
151
        setlocale(LC_ALL, $locale);
152
153
        \bindtextdomain(self::DOMAIN, self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin'
154
            .DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Lang');
155
        \bind_textdomain_codeset(self::DOMAIN, 'UTF-8');
156
        \textdomain(self::DOMAIN);
157
    }
158
159
    /**
160
     * Return the entity manager
161
     *
162
     * @return EntityManager
163
     */
164
    public static function getEntityManager()
165
    {
166
        return self::$entityManager;
167
    }
168
169
    /**
170
     * Get a configuration value
171
     *
172
     * @param null $key Optional: config value key
173
     * @return mixed Configuration value(s)
174
     */
175 1
    public static function getConfig($key = null)
176
    {
177 1
        if ($key === null) {
178
            return self::$config;
179
        }
180 1
        $keyParts = explode('.', $key);
181 1
        $config =& self::$config;
182 1
        foreach ($keyParts as $keyPart) {
183 1
            if (!array_key_exists($keyPart, $config)) {
184
                throw new \InvalidArgumentException(sprintf('Invalid config key "%s"', $key), 1466179561);
185
            }
186 1
            $config =& $config[$keyPart];
187 1
        }
188 1
        return $config;
189
    }
190
191
    /**
192
     * Return the contents of a particular template
193
     *
194
     * @param string $template Template name
195
     * @return string template contents
196
     */
197
    public static function getTemplate($template)
198
    {
199
        $templateFile = self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin'
200
            .DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Templates'.DIRECTORY_SEPARATOR.$template;
201
        if (!file_exists($templateFile)) {
202
            throw new \RuntimeException(sprintf('Unknown template "%s"', $template), 1475503926);
203
        }
204
        return file_get_contents($templateFile);
205
    }
206
207
    /**
208
     * Return the account service
209
     *
210
     * @return AccountService Account service
211
     */
212
    public static function getAccountService()
213
    {
214
        if (self::$accountService === null) {
215
            $storageAdapter = new DoctrineStorageAdapterStrategy();
216
            $persistenceAdapterFactory = new PersistenceAdapterFactory();
217
            self::$accountService = new AccountService($storageAdapter, $persistenceAdapterFactory);
218
        }
219
220
        return self::$accountService;
221
    }
222
223
    /**
224
     * Return the virtual host service
225
     *
226
     * @return VirtualHostService Virtual host service
227
     */
228
    public static function getVirtualHostService()
229
    {
230
        if (self::$accountService === null) {
231
            $storageAdapter = new DoctrineStorageAdapterStrategy();
232
            $persistenceAdapterFactory = new PersistenceAdapterFactory();
233
            self::$vhostService = new VirtualHostService($storageAdapter, $persistenceAdapterFactory);
0 ignored issues
show
Unused Code introduced by
The call to VirtualHostService::__construct() has too many arguments starting with $storageAdapter.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
234
        }
235
236
        return self::$vhostService;
237
    }
238
}
239