Completed
Push — master ( e6b3d9...94ffcd )
by Park Jong-Hun
03:09
created

Application   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 20
Bugs 0 Features 2
Metric Value
wmc 13
c 20
b 0
f 2
lcom 1
cbo 4
dl 0
loc 100
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 10 2
A setSiteConfig() 0 4 1
A setErrorReporterConfig() 0 4 1
A setDbConfig() 0 4 1
A setViewEngineConfig() 0 4 1
A setDisplayError() 0 5 1
A registerErrorReporters() 0 7 1
A setRouterConfig() 0 4 1
A dispatch() 0 7 1
A url() 0 4 1
A getEntityManager() 0 8 1
1
<?php
2
3
namespace Core;
4
5
use Prob\Rewrite\Request;
6
use Doctrine\ORM\Tools\Setup;
7
use Doctrine\ORM\EntityManager;
8
9
class Application
10
{
11
    private $routerConfig = [];
12
    private $siteConfig = [];
13
    private $errorReporterConfig = [];
14
    private $viewEngineConfig = [];
15
    private $dbConfig = [];
16
17
    /**
18
     * Singleton: private constructor
19
     */
20
    private function __construct()
21
    {
22
    }
23
24
    /**
25
     * @return Application
26
     */
27
    public static function getInstance()
28
    {
29
        static $instance = null;
30
31
        if ($instance === null) {
32
            $instance = new self();
33
        }
34
35
        return $instance;
36
    }
37
38
    public function setSiteConfig(array $siteConfig)
39
    {
40
        $this->siteConfig = $siteConfig;
41
    }
42
43
    public function setErrorReporterConfig(array $errorReporterConfig)
44
    {
45
        $this->errorReporterConfig = $errorReporterConfig;
46
    }
47
48
    public function setDbConfig(array $dbConfig)
49
    {
50
        $this->dbConfig = $dbConfig;
51
    }
52
53
    public function setViewEngineConfig(array $viewEngineConfig)
54
    {
55
        $this->viewEngineConfig = $viewEngineConfig;
56
    }
57
58
    public function setDisplayError($isDisplay)
59
    {
60
        error_reporting(E_ALL);
61
        ini_set('display_errors', $isDisplay);
62
    }
63
64
    public function registerErrorReporters()
65
    {
66
        $register = new ErrorReporterRegister();
67
        $register->setErrorReporterConfig($this->errorReporterConfig);
68
        $register->setEnabledReporters($this->siteConfig['errorReporters']);
69
        $register->regist();
70
    }
71
72
    public function setRouterConfig(array $routerConfig)
73
    {
74
        $this->routerConfig = $routerConfig;
75
    }
76
77
    public function dispatch(Request $request)
78
    {
79
        $dispatcher = new AppDispatcher();
80
        $dispatcher->setRouterConfig($this->routerConfig);
81
        $dispatcher->setViewEngineConfig($this->viewEngineConfig[$this->siteConfig['viewEngine']]);
82
        $dispatcher->dispatch($request);
83
    }
84
85
86
    /**
87
     * Return url path with site url
88
     * @param  string $url sub url
89
     * @return string
90
     */
91
    public function url($url = '')
92
    {
93
        return $this->siteConfig['url'] . $url;
94
    }
95
96
97
    /**
98
     * @return EntityManager
99
     */
100
    public function getEntityManager()
101
    {
102
        $config = Setup::createAnnotationMetadataConfiguration(
103
                    $this->dbConfig['entityPath'],
104
                    $this->dbConfig['devMode']
105
                    );
106
        return EntityManager::create($this->dbConfig[$this->siteConfig['database']], $config);
107
    }
108
}
109