Bootstrap   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 249
Duplicated Lines 3.61 %

Coupling/Cohesion

Components 4
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 9
loc 249
ccs 77
cts 77
cp 1
rs 10
c 0
b 0
f 0
wmc 29
lcom 4
cbo 12

19 Methods

Rating   Name   Duplication   Size   Complexity  
A defineTimezone() 0 5 1
A initHelperDb() 0 7 1
A setSession() 0 5 1
A setRequest() 0 5 1
A getRequest() 0 7 2
A hasRequest() 0 4 1
A setFlashMessenger() 0 5 1
A isWarmed() 0 4 1
A setIsWarmed() 0 5 1
A setCookie() 0 5 1
A warmUp() 0 12 2
A getSession() 0 7 2
A getLogger() 0 12 2
A setLogger() 0 8 2
A registerErrorHandler() 0 8 2
A getFlashMessenger() 0 7 2
A getEnvironment() 9 9 2
A setEnvironment() 0 8 2
A getCookie() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace FMUP;
3
4
class Bootstrap implements Environment\OptionalInterface, Sapi\OptionalInterface, Logger\LoggerInterface
5
{
6
    use Environment\OptionalTrait {
7
        getEnvironment as getEnvironmentTrait;
8
        setEnvironment as setEnvironmentTrait;
9
    }
10
    use Sapi\OptionalTrait;
11
    use Config\RequiredTrait;
12
    use Logger\LoggerTrait {
13
        getLogger as getLoggerTrait;
14
        setLogger as setLoggerTrait;
15
    }
16
17
    private $isErrorHandlerRegistered = false;
18
    private $request;
19
    private $session;
20
    private $flashMessenger;
21
    private $isWarmed;
22
    /**
23
     * @var Cookie
24
     */
25
    private $cookie;
26
27
    /**
28
     * Prepare needed configuration in bootstrap.
29
     *
30
     * There is no need to warm up DB connection but it could be configured
31
     *
32
     * @return $this
33
     */
34 1
    public function warmUp()
35
    {
36 1
        if (!$this->isWarmed()) {
37 1
            $this->defineTimezone();
38 1
            $this->getLogger();
39 1
            $this->initHelperDb();
40 1
            $this->getEnvironment();
41
            //$this->registerErrorHandler(); //@todo activation of this might be very useful
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42 1
            $this->setIsWarmed();
43
        }
44 1
        return $this;
45
    }
46
47
    /**
48
     * Define default timezone
49
     * @return $this
50
     * @codeCoverageIgnore
51
     */
52
    protected function defineTimezone()
53
    {
54
        date_default_timezone_set("Europe/Paris");
55
        return $this;
56
    }
57
58
    /**
59
     * Initialize Config in helper db
60
     * @return $this
61
     */
62 1
    private function initHelperDb()
63
    {
64 1
        Db\Manager::getInstance()
65 1
            ->setConfig($this->getConfig())//@todo find a better solution
66 1
            ->setLogger($this->getLogger());
67 1
        return $this;
68
    }
69
70
    /**
71
     * @return Session
72
     */
73 2
    public function getSession()
74
    {
75 2
        if (!$this->session) {
76 1
            $this->session = Session::getInstance();
77
        }
78 2
        return $this->session;
79
    }
80
81
    /**
82
     * Define session component
83
     * @param Session $session
84
     * @return $this
85
     */
86 2
    public function setSession(Session $session)
87
    {
88 2
        $this->session = $session;
89 2
        return $this;
90
    }
91
92
    /**
93
     * Return logger
94
     * @return Logger
95
     */
96 2
    public function getLogger()
97
    {
98 2
        if (!$this->hasLogger()) {
99 2
            $this->setLogger(
100 2
                (new Logger())
101 2
                    ->setRequest($this->getRequest())
102 2
                    ->setConfig($this->getConfig())
103 2
                    ->setEnvironment($this->getEnvironment())
104
            );
105
        }
106 2
        return $this->getLoggerTrait();
107
    }
108
109
    /**
110
     * Define logger
111
     * @param Logger $logger
112
     * @return $this
113
     */
114 2
    public function setLogger(Logger $logger)
115
    {
116 2
        if (!$logger->hasEnvironment()) {
117 1
            $logger->setEnvironment($this->getEnvironment());
118
        }
119 2
        $this->setLoggerTrait($logger);
120 2
        return $this;
121
    }
122
123 1
    public function registerErrorHandler()
124
    {
125 1
        if (!$this->isErrorHandlerRegistered) {
126 1
            \Monolog\ErrorHandler::register($this->getLogger()->get(\FMUP\Logger\Channel\System::NAME)->getLogger());
127 1
            $this->isErrorHandlerRegistered = true;
128
        }
129 1
        return $this;
130
    }
131
132
    /**
133
     * Define HTTP request object
134
     * @param Request $request
135
     * @return $this
136
     */
137 1
    public function setRequest(Request $request)
138
    {
139 1
        $this->request = $request;
140 1
        return $this;
141
    }
142
143
    /**
144
     * Retrieve defined HTTP request object
145
     * @return Request
146
     * @throws \LogicException if no request has been set
147
     */
148 2
    public function getRequest()
149
    {
150 2
        if (!$this->hasRequest()) {
151 1
            throw new \LogicException('Request is not defined');
152
        }
153 1
        return $this->request;
154
    }
155
156
    /**
157
     * Check if request is defined
158
     * @return bool
159
     */
160 2
    public function hasRequest()
161
    {
162 2
        return !is_null($this->request);
163
    }
164
165
    /**
166
     * Get flashMessenger
167
     * @return \FMUP\FlashMessenger
168
     */
169 1
    public function getFlashMessenger()
170
    {
171 1
        if ($this->flashMessenger === null) {
172 1
            $this->flashMessenger = FlashMessenger::getInstance();
173
        }
174 1
        return $this->flashMessenger;
175
    }
176
177
    /**
178
     * @param FlashMessenger $flashMessenger
179
     * @return $this
180
     */
181 1
    public function setFlashMessenger(FlashMessenger $flashMessenger)
182
    {
183 1
        $this->flashMessenger = $flashMessenger;
184 1
        return $this;
185
    }
186
187
    /**
188
     * @return bool
189
     */
190 1
    public function isWarmed()
191
    {
192 1
        return (bool)$this->isWarmed;
193
    }
194
195
    /**
196
     * @return $this
197
     */
198 1
    public function setIsWarmed()
199
    {
200 1
        $this->isWarmed = true;
201 1
        return $this;
202
    }
203
204
    /**
205
     * @return Environment
206
     */
207 2 View Code Duplication
    public function getEnvironment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209 2
        if (!$this->hasEnvironment()) {
210 2
            $environment = Environment::getInstance();
211 2
            $environment->setConfig($this->getConfig());
212 2
            $this->setEnvironmentTrait($environment);
213
        }
214 2
        return $this->getEnvironmentTrait();
215
    }
216
217
    /**
218
     * @param Environment $environment
219
     * @return $this
220
     */
221 1
    public function setEnvironment(Environment $environment = null)
222
    {
223 1
        if (!$environment->hasConfig()) {
224 1
            $environment->setConfig($this->getConfig());
0 ignored issues
show
Bug introduced by
It seems like $environment is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
225
        }
226 1
        $this->setEnvironmentTrait($environment);
227 1
        return $this;
228
    }
229
230
    /**
231
     * Retriever Cookie component
232
     * @return Cookie
233
     */
234 1
    public function getCookie()
235
    {
236 1
        if (!$this->cookie) {
237 1
            $this->cookie = Cookie::getInstance();
238
        }
239 1
        return $this->cookie;
240
    }
241
242
    /**
243
     * Define cookie component
244
     * @param Cookie $cookie
245
     * @return $this
246
     */
247 1
    public function setCookie(Cookie $cookie)
248
    {
249 1
        $this->cookie = $cookie;
250 1
        return $this;
251
    }
252
}
253