Utility   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 170
Duplicated Lines 11.18 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 19
loc 170
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
B setCookie() 0 24 1
A unsetCookie() 0 7 1
A publicCache() 10 10 1
A privateNoExpireCache() 9 9 1
A privateCache() 0 7 1
A noCache() 0 11 1
A timeStamp() 0 4 1
A addExpire() 0 7 2
A addDomain() 0 6 2
A addPath() 0 6 2
A addSecure() 0 6 2
A addHttpOnly() 0 6 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
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Middleware
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Middleware;
16
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * Utility
21
 *
22
 * Utilities under PSR-7, set cookie etc.
23
 *
24
 * Modified from Relay.Middleware/blob/1.x/src/SessionHeadersHandler.php
25
 *
26
 * @package Phossa2\Middleware
27
 * @author  Hong Zhang <[email protected]>
28
 * @version 2.1.0
29
 * @since   2.1.0 added
30
 */
31
class Utility
32
{
33
    /**
34
     * Set a cookie in the response
35
     *
36
     * @param  ResponseInterface $response
37
     * @param  string $name
38
     * @param  string $value
39
     * @param  int $ttl
40
     * @param  string $path
41
     * @param  string $domain
42
     * @param  bool $secure
43
     * @param  bool $httponly
44
     * @return ResponseInterface
45
     * @access public
46
     */
47
    public static function setCookie(
48
        ResponseInterface $response,
49
        /*# string */ $name,
50
        /*# string */ $value = null,
51
        /*# int */ $ttl = null,
52
        /*# string */ $path = null,
53
        /*# string */ $domain = null,
54
        /*# bool */ $secure = false,
55
        /*# bool */ $httponly = true
56
    )/*# : ResponseInterface */ {
57
        $cookie = urlencode($name) . '=' . urlencode($value);
58
59
        self::addExpire($cookie, $ttl);
60
61
        self::addDomain($cookie, $domain);
62
63
        self::addPath($cookie, $path);
64
65
        self::addSecure($cookie, $secure);
66
67
        self::addHttpOnly($cookie, $httponly);
68
69
        return $response->withAddedHeader('Set-Cookie', $cookie);
70
    }
71
72
    /**
73
     * Unset a cookie
74
     *
75
     * @param  ResponseInterface $response
76
     * @param  string $name
77
     * @param  string $path
78
     * @return ResponseInterface
79
     * @access public
80
     */
81
    public static function unsetCookie(
82
        ResponseInterface $response,
83
        /*# string */ $name,
84
        /*# string */ $path = null
85
    )/*# : ResponseInterface */ {
86
        return self::setCookie($response, $name, '', time() - 86400, $path);
87
    }
88
89
    /**
90
     * Set public cache header
91
     *
92
     * @param  ResponseInterface $response
93
     * @param  int $cacheTime cache time in minutes
94
     * @return ResponseInterface
95
     * @access public
96
     */
97 View Code Duplication
    public static function publicCache(
98
        ResponseInterface $response,
99
        /*# int */ $cacheTime = 120
100
    )/*# : ResponseInterface */ {
101
        $maxAge = $cacheTime * 60;
102
        return $response
103
            ->withAddedHeader('Expires', self::timeStamp($maxAge))
104
            ->withAddedHeader('Cache-Control', "public, max-age={$maxAge}")
105
            ->withAddedHeader('Last-Modified', self::timeStamp());
106
    }
107
108
    /**
109
     * Set private_no_expire cache header
110
     *
111
     * @param  ResponseInterface $response
112
     * @param  int $cacheTime cache time in minutes
113
     * @return ResponseInterface
114
     * @access public
115
     */
116 View Code Duplication
    public static function privateNoExpireCache(
117
        ResponseInterface $response,
118
        /*# int */ $cacheTime = 120
119
    )/*# : ResponseInterface */ {
120
        $maxAge = $cacheTime * 60;
121
        return $response
122
            ->withAddedHeader('Cache-Control', "private, max-age={$maxAge}, pre-check={$maxAge}")
123
            ->withAddedHeader('Last-Modified', self::timeStamp());
124
    }
125
126
    /**
127
     * Set private cache header
128
     *
129
     * @param  ResponseInterface $response
130
     * @return ResponseInterface
131
     * @access protected
132
     */
133
    public static function privateCache(
134
        ResponseInterface $response
135
    )/*# : ResponseInterface */ {
136
        return self::privateNoExpireCache(
137
            $response->withAddedHeader('Expires', self::timeStamp(-3153600))
138
        );
139
    }
140
141
    /**
142
     * Set no cache header
143
     *
144
     * @param  ResponseInterface $response
145
     * @return ResponseInterface
146
     * @access public
147
     */
148
    public static function noCache(
149
        ResponseInterface $response
150
    )/*# : ResponseInterface */ {
151
        return $response
152
            ->withAddedHeader('Expires', self::timeStamp(-3153600))
153
            ->withAddedHeader(
154
                'Cache-Control',
155
                'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
156
            )
157
            ->withAddedHeader('Pragma', 'no-cache');
158
    }
159
160
    protected static function timeStamp(/*# int */ $ttl= 0)
161
    {
162
        return gmdate('D, d M Y H:i:s T', time() + $ttl);
163
    }
164
165
    protected static function addExpire(/*# string */ &$cookie, $ttl)
166
    {
167
        if ($ttl) {
168
            $expires = self::timeStamp($ttl);
169
            $cookie .= "; expires={$expires}; max-age={$ttl}";
170
        }
171
    }
172
173
    protected static function addDomain(/*# string */ &$cookie, $domain)
174
    {
175
        if ($domain) {
176
            $cookie .= "; domain={$domain}";
177
        }
178
    }
179
180
    protected static function addPath(/*# string */ &$cookie, $path)
181
    {
182
        if ($path) {
183
            $cookie .= "; path={$path}";
184
        }
185
    }
186
187
    protected static function addSecure(/*# string */ &$cookie, $secure)
188
    {
189
        if ($secure) {
190
            $cookie .= '; secure';
191
        }
192
    }
193
194
    protected static function addHttpOnly(/*# string */ &$cookie, $httponly)
195
    {
196
        if ($httponly) {
197
            $cookie .= '; httponly';
198
        }
199
    }
200
}
201