Completed
Push — master ( a34111...b893df )
by Park Jong-Hun
14:20
created

Application   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 21
Bugs 0 Features 2
Metric Value
wmc 17
c 21
b 0
f 2
lcom 2
cbo 6
dl 0
loc 128
rs 10

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