Completed
Pull Request — master (#186)
by Vladimir
06:00 queued 03:00
created

Service   B

Complexity

Total Complexity 24

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 4

Test Coverage

Coverage 75.51%

Importance

Changes 0
Metric Value
wmc 24
lcom 7
cbo 4
dl 0
loc 232
ccs 37
cts 49
cp 0.7551
rs 8.3333
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A setQueryBuilderConfig() 0 4 1
A getQueryBuilderConnection() 0 8 2
A setRequest() 0 4 1
A getRequest() 0 10 2
A setGenerator() 0 4 1
A getGenerator() 0 4 1
A getParameter() 0 4 1
A setSession() 0 4 1
A getSession() 0 4 1
A getNewSession() 0 7 1
A getFormFactory() 0 4 1
A setFormFactory() 0 4 1
A getEnvironment() 0 4 1
A setEnvironment() 0 4 1
A getModelCache() 0 4 1
A setModelCache() 0 4 1
A getSiteThemes() 0 4 1
A getDefaultSiteTheme() 0 6 1
A getContainer() 0 4 1
A setKernel() 0 4 1
A getDispatcher() 0 4 1
A isDebug() 0 4 1
1
<?php
2
/**
3
 * This file contains functionality relating Symfony2 components such as the template engine, requests, and sessions
4
 *
5
 * @package    BZiON
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
use BZIon\Cache\ModelCache;
10
use Pecee\Pixie\Connection;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcher;
13
use Symfony\Component\Form\FormFactory;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Session\Session;
16
use Symfony\Component\HttpFoundation\Session\SessionInterface;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
19
/**
20
 * Class Service
21
 */
22
abstract class Service
23
{
24
    /**
25
     * Symfony's URL Generator
26
     * @var UrlGeneratorInterface;
27
     */
28
    private static $generator;
29
30
    /**
31
     * Symfony's Request class
32
     * @var Request
33
     */
34
    private static $request;
35
36
    /**
37
     * Symfony's FormFactory
38
     * @var FormFactory
39
     */
40
    private static $formFactory;
41
42
    /**
43
     * The kernel's environment (prod, debug, profile or test)
44
     * @var string
45
     */
46
    private static $environment;
47
48
    /**
49
     * The model memory cache
50
     * @var ModelCache
51
     */
52
    private static $modelCache;
53
54
    /**
55
     * The AppKernel's container
56
     * @var AppKernel
57
     */
58
    private static $kernel;
59
60
    private static $qbConnection;
61
    private static $qbConfig;
62 23
63
    public static function setQueryBuilderConfig(array $config)
64 23
    {
65 23
        self::$qbConfig = $config;
66
    }
67
68
    public static function getQueryBuilderConnection()
69
    {
70 23
        if (!self::$qbConnection) {
71
            self::$qbConnection = new Connection('mysql', self::$qbConfig);
72 23
        }
73
74
        return self::$qbConnection;
75
    }
76
77
    /**
78 23
     * @param Request $request
79
     */
80
    public static function setRequest($request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
81
    {
82
        self::$request = $request;
83
    }
84
85
    /**
86 1
     * @return Request
87
     */
88 1
    public static function getRequest()
89 1
    {
90
        if (!self::$request) {
91
            $request = Request::createFromGlobals();
92
            $request->setSession(self::getNewSession());
93
            self::setRequest($request);
94 1
        }
95
96 1
        return self::$request;
97
    }
98
99
    /**
100
     * Sets the URL Generator.
101
     * @param  UrlGeneratorInterface $generator
102
     * @return void
103
     */
104
    public static function setGenerator($generator)
105 76
    {
106
        self::$generator = $generator;
107 76
    }
108
109
    /**
110
     * @return UrlGeneratorInterface
111
     */
112
    public static function getGenerator()
113
    {
114
        return self::$generator;
115
    }
116
117
    /**
118
     * Gets a parameter
119
     *
120
     * @param  string $name The parameter name
121
     * @return mixed  The parameter value
122
     */
123
    public static function getParameter($name)
124
    {
125
        return self::getContainer()->getParameter($name);
126
    }
127
128
    /**
129
     * @param SessionInterface $session
130
     */
131
    public static function setSession($session)
132
    {
133
        self::getRequest()->setSession($session);
134
    }
135
136
    /**
137
     * @return SessionInterface
138
     */
139
    public static function getSession()
140
    {
141 1
        return self::getRequest()->getSession();
142
    }
143 1
144
    /**
145
     * Create a new session
146
     * @return Session
147
     */
148
    public static function getNewSession()
149 23
    {
150
        $newSession = new Session();
151 23
        $newSession->start();
152 23
153
        return $newSession;
154
    }
155
156
    /**
157 1
     * @return FormFactory
158
     */
159 1
    public static function getFormFactory()
160
    {
161
        return self::$formFactory;
162
    }
163
164
    /**
165 1
     * @param FormFactory $formFactory
166
     */
167 1
    public static function setFormFactory($formFactory)
168 1
    {
169
        self::$formFactory = $formFactory;
170
    }
171
172
    /**
173 76
     * @return string
174
     */
175 76
    public static function getEnvironment()
176
    {
177
        return self::$environment;
178
    }
179
180
    /**
181 1
     * @param string $environment
182
     */
183 1
    public static function setEnvironment($environment)
184 1
    {
185
        self::$environment = $environment;
186
    }
187
188
    /**
189 76
     * @return ModelCache
190
     */
191 76
    public static function getModelCache()
192
    {
193
        return self::$modelCache;
194
    }
195
196
    /**
197 1
     * @param ModelCache $modelCache
198
     */
199 1
    public static function setModelCache($modelCache)
200 1
    {
201
        self::$modelCache = $modelCache;
202
    }
203
204
    /**
205 1
     * @return string[][]
206
     */
207 1
    public static function getSiteThemes()
208
    {
209
        return self::getParameter('bzion.site.themes');
210
    }
211
212
    /**
213 76
     * @return string
214
     */
215 76
    public static function getDefaultSiteTheme()
216
    {
217
        $themes = self::getSiteThemes();
218
219
        return $themes[0]['slug'];
220
    }
221
222
    /**
223
     * @return ContainerInterface
224
     */
225
    public static function getContainer()
226
    {
227
        return self::$kernel->getContainer();
228
    }
229
230
    /**
231
     * @param $kernel
232
     */
233
    public static function setKernel($kernel)
234
    {
235
        self::$kernel = $kernel;
236
    }
237
238
    /**
239
     * @return EventDispatcher
240
     */
241
    public static function getDispatcher()
242
    {
243
        return self::getContainer()->get('event_dispatcher');
244
    }
245
246
    /**
247
     * @return bool
248
     */
249
    public static function isDebug()
250
    {
251
        return self::getParameter('kernel.debug');
252
    }
253
}
254