Completed
Push — master ( 8af507...d2cd75 )
by Konstantinos
07:28
created

Service::setTemplateEngine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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 Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\EventDispatcher\EventDispatcher;
11
use Symfony\Component\Form\FormFactory;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Session\Session;
14
use Symfony\Component\HttpFoundation\Session\SessionInterface;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
17
/**
18
 * Class Service
19
 */
20
abstract class Service
21
{
22
    /**
23
     * Symfony's URL Generator
24
     * @var UrlGeneratorInterface;
25
     */
26
    private static $generator;
27
28
    /**
29
     * Symfony's Request class
30
     * @var Request
31
     */
32
    private static $request;
33
34
    /**
35
     * Symfony's FormFactory
36
     * @var FormFactory
37
     */
38
    private static $formFactory;
39
40
    /**
41
     * The kernel's environment (prod, debug, profile or test)
42
     * @var string
43
     */
44
    private static $environment;
45
46
    /**
47
     * The model memory cache
48
     * @var ModelCache
49
     */
50
    private static $modelCache;
51
52
    /**
53
     * The AppKernel's container
54
     * @var AppKernel
55
     */
56
    private static $kernel;
57
58
    /**
59
     * @param Request $request
60
     */
61
    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...
62
    {
63
        self::$request = $request;
64
    }
65
66
    /**
67 1
     * @return Request
68
     */
69 1
    public static function getRequest()
70 1
    {
71
        if (!self::$request) {
72
            $request = Request::createFromGlobals();
73
            $request->setSession(self::getNewSession());
74
            self::setRequest($request);
75 1
        }
76
77 1
        return self::$request;
78
    }
79
80
    /**
81
     * Sets the URL Generator.
82
     * @param  UrlGeneratorInterface $generator
83 1
     * @return void
84
     */
85
    public static function setGenerator($generator)
86
    {
87
        self::$generator = $generator;
88
    }
89
90
    /**
91 1
     * @return UrlGeneratorInterface
92
     */
93 1
    public static function getGenerator()
94 1
    {
95
        return self::$generator;
96
    }
97
98
    /**
99 1
     * Gets a parameter
100
     *
101 1
     * @param  string $name The parameter name
102
     * @return string The parameter value
103
     */
104
    public static function getParameter($name)
105
    {
106
        return self::getContainer()->getParameter($name);
107
    }
108
109
    /**
110 39
     * @param SessionInterface $session
111
     */
112 39
    public static function setSession($session)
113
    {
114
        self::getRequest()->setSession($session);
115
    }
116
117
    /**
118
     * @return SessionInterface
119
     */
120
    public static function getSession()
121
    {
122
        return self::getRequest()->getSession();
123
    }
124
125
    /**
126
     * Create a new session
127
     * @return Session
128
     */
129
    public static function getNewSession()
130
    {
131
        $newSession = new Session();
132
        $newSession->start();
133
134
        return $newSession;
135
    }
136
137
    /**
138
     * @return FormFactory
139
     */
140
    public static function getFormFactory()
141
    {
142
        return self::$formFactory;
143
    }
144
145
    /**
146 1
     * @param FormFactory $formFactory
147
     */
148 1
    public static function setFormFactory($formFactory)
149
    {
150
        self::$formFactory = $formFactory;
151
    }
152
153
    /**
154 1
     * @return string
155
     */
156 1
    public static function getEnvironment()
157 1
    {
158
        return self::$environment;
159
    }
160
161
    /**
162 1
     * @param string $environment
163
     */
164 1
    public static function setEnvironment($environment)
165
    {
166
        self::$environment = $environment;
167
    }
168
169
    /**
170 1
     * @return ModelCache
171
     */
172 1
    public static function getModelCache()
173 1
    {
174
        return self::$modelCache;
175
    }
176
177
    /**
178 1
     * @param BZIon\Cache\ModelCache $modelCache
179
     */
180 1
    public static function setModelCache($modelCache)
181
    {
182
        self::$modelCache = $modelCache;
0 ignored issues
show
Documentation Bug introduced by
It seems like $modelCache of type object<BZIon\Cache\ModelCache> is incompatible with the declared type object<ModelCache> of property $modelCache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
183
    }
184
185
    /**
186 1
     * @return ContainerInterface
187
     */
188 1
    public static function getContainer()
189 1
    {
190
        return self::$kernel->getContainer();
191
    }
192
193
    /**
194 39
     * @param $kernel
195
     */
196 39
    public static function setKernel($kernel)
197
    {
198
        self::$kernel = $kernel;
199
    }
200
201
    /**
202 1
     * @return EventDispatcher
203
     */
204 1
    public static function getDispatcher()
205 1
    {
206
        return self::getContainer()->get('event_dispatcher');
207
    }
208
209
    /**
210 39
     * @return bool
211
     */
212 39
    public static function isDebug()
213
    {
214
        return self::getParameter('kernel.debug');
215
    }
216
}
217