Passed
Push — master ( 710aff...9afc87 )
by Diogo Oliveira de
01:38
created

Bootstrap::generateApp()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
1
<?php
2
3
namespace SeedApp;
4
5
use RedBeanPHP\R;
6
use Slim\App;
7
use Slim\Container;
8
use SeedApp\Controllers\DefaultController;
9
use SeedApp\DependencyManager;
10
use SeedApp\Middleware;
11
12
class Bootstrap
13
{
14
    protected $app;
15
16
    public function __construct()
17
    {
18
        $this->app = null;
19
    }
20
21
    public function generateApp(): App
22
    {
23
        if (null !== $this->app) {
24
            return $this->app;
25
        }
26
27
        $settings = require __DIR__ . '/../../config/application.php';
28
        $container = new Container($settings);
29
30
        $conf = $container->get('settings')['db'];
31
        $strConn = "mysql:host={$conf['host']};dbname={$conf['dbname']}";
32
        if (!R::testConnection()) {
33
            R::setup($strConn, $conf['user'], $conf['pass']);
34
            R::ext('xdispense', function ($type) {
35
                return R::getRedBean()->dispense($type);
36
            });
37
            R::freeze(true);
38
        }
39
40
        $app = new App($container);
41
42
        $dependencyManager = new DependencyManager($app);
43
        $dependencyManager->loadDependencies();
44
45
        $middleware = new Middleware($app);
46
        $middleware->loadMiddleware();
47
48
        $controller = new DefaultController($app);
49
        $controller->loadActions();
50
51
        $this->app = $app;
52
        return $this->app;
53
    }
54
}
55