Completed
Push — master ( a3911e...bb372d )
by Joschi
02:57
created

App   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 67.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
lcom 2
cbo 8
dl 0
loc 162
ccs 38
cts 56
cp 0.6786
rs 10
c 1
b 0
f 0

6 Methods

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