Completed
Push — 4.0 ( ad8a54...ed771a )
by Marco
06:33
created

Model::consolidate()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 19.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
ccs 12
cts 24
cp 0.5
rs 4.909
cc 9
eloc 19
nc 48
nop 2
crap 19.125
1
<?php namespace Comodojo\Dispatcher\Response;
2
3
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
4
use \Comodojo\Dispatcher\Components\Configuration;
5
use \Comodojo\Dispatcher\Response\Headers;
6
use \Comodojo\Dispatcher\Response\Status;
7
use \Comodojo\Dispatcher\Response\Content;
8
use \Comodojo\Dispatcher\Response\Location;
9
use \Comodojo\Dispatcher\Request\Model as Request;
10
use \Comodojo\Dispatcher\Router\Route;
11
use \Comodojo\Cookies\CookieManager;
12
use \Psr\Log\LoggerInterface;
13
14
/**
15
 * @package     Comodojo Dispatcher
16
 * @author      Marco Giovinazzi <[email protected]>
17
 * @author      Marco Castiello <[email protected]>
18
 * @license     GPL-3.0+
19
 *
20
 * LICENSE:
21
 *
22
 * This program is free software: you can redistribute it and/or modify
23
 * it under the terms of the GNU Affero General Public License as
24
 * published by the Free Software Foundation, either version 3 of the
25
 * License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License
33
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34
 */
35
36
class Model extends DispatcherClassModel {
37
38
    private $headers;
39
40
    private $cookies;
41
42
    private $status;
43
44
    private $content;
45
46
    private $location;
47
48
    private $content_type = "text/plain";
0 ignored issues
show
Unused Code introduced by
The property $content_type is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
50
    private $charset;
0 ignored issues
show
Unused Code introduced by
The property $charset is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
51
52 3
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
53
54 3
        parent::__construct($configuration, $logger);
55
56 3
        $this->headers = new Headers();
57
58 3
        $this->cookies = new CookieManager();
59
60 3
        $this->status = new Status();
61
62 3
        $this->content = new Content();
63
64 3
        $this->location = new Location();
65
66 3
    }
67
68 2
    public function headers() {
69
70 2
        return $this->headers;
71
72
    }
73
74 2
    public function cookies() {
75
76 2
        return $this->cookies;
77
78
    }
79
80 4
    public function status() {
81
82 4
        return $this->status;
83
84
    }
85
86 4
    public function content() {
87
88 4
        return $this->content;
89
90
    }
91
92 2
    public function location() {
93
94 2
        return $this->location;
95
96
    }
97
98 1
    public function consolidate(Request $request, Route $route = null) {
99
100 1
        $status = $this->status()->get();
101
102 1
        $output_class_name = "\\Comodojo\\Dispatcher\\Response\\Preprocessor\\Status".$status;
103
104
        // @TODO: this condition will be removed when all preprocessors ready
105 1
        if ( class_exists($output_class_name) ) {
106 1
            $output = new $output_class_name($this);
107 1
        } else {
108
            $output = new \Comodojo\Dispatcher\Response\Preprocessor\Status200($this);
109
        }
110
111 1
        $output->consolidate();
112
113 1
        if ( $route != null ) {
114
            $this->setClientCache($request, $route);
115
        }
116
117
        // extra checks
118
119 1
        if ( $request->method()->get() == 'HEAD' && !in_array($status, array(100,101,102,204,304)) ) {
120
            $length = $this->content()->length();
121
            $this->content()->set(null);
122
            if ($length) $this->headers()->set('Content-Length', $length);
123
        }
124
125 1
        if ($this->headers()->get('Transfer-Encoding') != null) {
126
            $this->headers()->delete('Content-Length');
127
        }
128
129 1
        if ( $request->version()->get() == '1.0' && false !== strpos($this->headers->get('Cache-Control'), 'no-cache')) {
130
            $this->headers()->set('pragma', 'no-cache');
131
            $this->headers()->set('expires', -1);
132
        }
133
134 1
    }
135
136
    private function setClientCache(Request $request, Route $route) {
137
138
        $cache = strtoupper($route->getParameter('cache'));
139
        $ttl = $route->getParameter('ttl');
140
141
        if (
142
            ($cache == 'CLIENT' || $cache == 'BOTH') &&
143
            in_array($request->method()->get(), array('GET', 'HEAD', 'POST', 'PUT')) &&
144
            in_array($this->status()->get(), array(200, 203, 300, 301, 302, 404, 410))
145
            // @TODO: here we should also check for Cache-Control no-store or private;
146
            //        the cache layer will be improoved in future versions.
147
        ) {
148
149
            if ( $ttl > 0 ) {
150
151
                $this->headers()->set("Cache-Control","max-age=".$ttl.", must-revalidate");
152
                $this->headers()->set("Expires",gmdate("D, d M Y H:i:s", (int)$this->getTimestamp() + $ttl)." GMT");
0 ignored issues
show
Bug introduced by
The method getTimestamp() does not seem to exist on object<Comodojo\Dispatcher\Response\Model>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
154
            } else {
155
156
                $this->headers()->set("Cache-Control","no-cache, must-revalidate");
157
                $this->headers()->set("Expires","Mon, 26 Jul 1997 05:00:00 GMT");
158
159
            }
160
161
        }
162
163
    }
164
165
}
166