Completed
Push — master ( 9003f0...ee041c )
by Joschi
04:37
created

App   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 13.64%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 216
c 1
b 0
f 0
wmc 18
lcom 1
cbo 10
ccs 9
cts 66
cp 0.1364
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A bootstrap() 0 16 1
B initializeDoctrine() 0 34 4
A getEntityManager() 0 4 1
A getConfig() 0 15 4
A getTemplate() 0 9 2
A getAccountService() 0 8 2
A getVirtualHostService() 0 8 2
A getDomainService() 0 8 2
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\Contract\PersistenceAdapterFactoryInterface;
44
use Tollwerk\Admin\Application\Contract\StorageAdapterStrategyInterface;
45
use Tollwerk\Admin\Application\Service\AccountService;
46
use Tollwerk\Admin\Application\Service\DomainService;
47
use Tollwerk\Admin\Application\Service\VirtualHostService;
48
use Tollwerk\Admin\Infrastructure\Doctrine\EnumVhosttypeType;
49
use Tollwerk\Admin\Infrastructure\Factory\PersistenceAdapterFactory;
50
use Tollwerk\Admin\Infrastructure\Strategy\DoctrineStorageAdapterStrategy;
51
52
/**
53
 * App
54
 *
55
 * @package Tollwerk\Admin
56
 * @subpackage Tollwerk\Admin\Ports
57
 */
58
class App
59
{
60
    /**
61
     * Configuration
62
     *
63
     * @var array
64
     */
65
    protected static $config;
66
    /**
67
     * Root directory
68
     *
69
     * @var string
70
     */
71
    protected static $rootDirectory;
72
    /**
73
     * Entity manager
74
     *
75
     * @var EntityManager
76
     */
77
    protected static $entityManager;
78
    /**
79
     * Developer mode
80
     *
81
     * @var boolean
82
     */
83
    protected static $devMode;
84
    /**
85
     * Account service
86
     *
87
     * @var AccountService
88
     */
89
    protected static $accountService = null;
90
    /**
91
     * Virtual host service
92
     *
93
     * @var VirtualHostService
94
     */
95
    protected static $vhostService = null;
96
    /**
97
     * Domain service
98
     *
99
     * @var DomainService
100
     */
101
    protected static $domainService = null;
102
    /**
103
     * Active Storage adapter
104
     *
105
     * @var StorageAdapterStrategyInterface
106
     */
107
    protected static $storageAdapter;
108
    /**
109
     * Persistence adapter factory
110
     *
111
     * @var PersistenceAdapterFactoryInterface
112
     */
113
    protected static $persistenceAdapterFactory;
114
    /**
115
     * App domain
116
     *
117
     * @var string
118
     */
119
    const DOMAIN = 'admin';
120
121
    /**
122
     * Bootstrap
123
     *
124
     * @see https://github.com/toggl/toggl_api_docs/blob/master/reports.md
125
     * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html
126
     *
127
     * @param bool $devMode Developer mode
128
     */
129
    public static function bootstrap($devMode = false)
130
    {
131
        self::$devMode = !!$devMode;
132
        self::$rootDirectory = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR;
133
134
        // Initialize the configuration
135
        $config = file_get_contents(self::$rootDirectory.'config'.DIRECTORY_SEPARATOR.'config.yml');
136
        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...
137
138
        // Initialize doctrine
139
        self::initializeDoctrine();
140
141
        // Register the Doctrine storage adapter and persistence adapter factory
142
        self::$storageAdapter = new DoctrineStorageAdapterStrategy();
143
        self::$persistenceAdapterFactory = new PersistenceAdapterFactory();
144
    }
145
146
    /**
147
     * Initialize Doctrine
148
     */
149
    protected static function initializeDoctrine()
150
    {
151
        // If the Doctrine parameters don't exist
152
        if (empty(self::$config['doctrine'])
153
            || !is_array(self::$config['doctrine'])
154
            || empty(self::$config['doctrine']['dbparams'])
155
        ) {
156
            throw new \InvalidArgumentException('Invalid Doctrine database parameters', 1466175889);
157
        }
158
159
        $modelPaths = [
160
            self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin'
161
            .DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Model'
162
        ];
163
        $dbParams = self::$config['doctrine']['dbparams'];
164
        $config = Setup::createAnnotationMetadataConfiguration($modelPaths, self::$devMode);
165
166
        self::$entityManager = EntityManager::create($dbParams, $config);
167
        $platform = self::$entityManager->getConnection()->getDatabasePlatform();
168
        $platform->registerDoctrineTypeMapping('enum', 'string');
169
170
        // Register the virtual host type declaration
171
        Type::addType(EnumVhosttypeType::ENUM_TYPE, EnumVhosttypeType::class);
172
173
        // Set the locale
174
        $locale = self::getConfig('general.locale');
175
        putenv('LC_ALL='.$locale);
176
        setlocale(LC_ALL, $locale);
177
178
        \bindtextdomain(self::DOMAIN, self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin'
179
            .DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Lang');
180
        \bind_textdomain_codeset(self::DOMAIN, 'UTF-8');
181
        \textdomain(self::DOMAIN);
182
    }
183
184
    /**
185
     * Return the entity manager
186
     *
187
     * @return EntityManager
188
     */
189
    public static function getEntityManager()
190
    {
191
        return self::$entityManager;
192
    }
193
194
    /**
195
     * Get a configuration value
196
     *
197
     * @param null $key Optional: config value key
198
     * @return mixed Configuration value(s)
199
     */
200 2
    public static function getConfig($key = null)
201
    {
202 2
        if ($key === null) {
203
            return self::$config;
204
        }
205 2
        $keyParts = explode('.', $key);
206 2
        $config =& self::$config;
207 2
        foreach ($keyParts as $keyPart) {
208 2
            if (!array_key_exists($keyPart, $config)) {
209
                throw new \InvalidArgumentException(sprintf('Invalid config key "%s"', $key), 1466179561);
210
            }
211 2
            $config =& $config[$keyPart];
212 2
        }
213 2
        return $config;
214
    }
215
216
    /**
217
     * Return the contents of a particular template
218
     *
219
     * @param string $template Template name
220
     * @return string template contents
221
     */
222
    public static function getTemplate($template)
223
    {
224
        $templateFile = self::$rootDirectory.'src'.DIRECTORY_SEPARATOR.'Admin'
225
            .DIRECTORY_SEPARATOR.'Infrastructure'.DIRECTORY_SEPARATOR.'Templates'.DIRECTORY_SEPARATOR.$template;
226
        if (!file_exists($templateFile)) {
227
            throw new \RuntimeException(sprintf('Unknown template "%s"', $template), 1475503926);
228
        }
229
        return file_get_contents($templateFile);
230
    }
231
232
    /**
233
     * Return the account service
234
     *
235
     * @return AccountService Account service
236
     */
237
    public static function getAccountService()
238
    {
239
        if (self::$accountService === null) {
240
            self::$accountService = new AccountService(self::$storageAdapter, self::$persistenceAdapterFactory);
241
        }
242
243
        return self::$accountService;
244
    }
245
246
    /**
247
     * Return the virtual host service
248
     *
249
     * @return VirtualHostService Virtual host service
250
     */
251
    public static function getVirtualHostService()
252
    {
253
        if (self::$vhostService === null) {
254
            self::$vhostService = new VirtualHostService(self::$storageAdapter, self::$persistenceAdapterFactory);
255
        }
256
257
        return self::$vhostService;
258
    }
259
260
    /**
261
     * Return the domain service
262
     *
263
     * @return DomainService Domain service
264
     */
265
    public static function getDomainService()
266
    {
267
        if (self::$domainService === null) {
268
            self::$domainService = new DomainService(self::$storageAdapter, self::$persistenceAdapterFactory);
269
        }
270
271
        return self::$domainService;
272
    }
273
}
274