Total Complexity | 46 |
Total Lines | 214 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ScwCookie often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ScwCookie, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class ScwCookie |
||
6 | { |
||
7 | public $config = []; |
||
8 | private $decisionMade = false; |
||
9 | private $choices = []; |
||
10 | |||
11 | public function __construct() |
||
17 | } |
||
18 | |||
19 | public function getChoices() |
||
32 | } |
||
33 | |||
34 | public static function encrypt($value) |
||
35 | { |
||
36 | $value = json_encode($value); |
||
37 | $return = base64_encode($value); |
||
38 | return $return; |
||
39 | } |
||
40 | |||
41 | public static function decrypt($value) |
||
47 | } |
||
48 | |||
49 | public function isAllowed($name) |
||
50 | { |
||
51 | $choices = $this->getChoices(); |
||
52 | return isset($choices[$name]) && $choices[$name] == 'allowed'; |
||
53 | } |
||
54 | |||
55 | public function isEnabled($name) |
||
59 | } |
||
60 | |||
61 | public function getConfig($name, $attribute) |
||
62 | { |
||
63 | return isset($this->config[$name]) && isset($this->config[$name][$attribute]) |
||
64 | ? $this->config[$name][$attribute] |
||
65 | : false; |
||
66 | } |
||
67 | |||
68 | public function output() |
||
69 | { |
||
70 | echo $this->getOutput(); |
||
71 | } |
||
72 | |||
73 | public function getOutput() |
||
74 | { |
||
75 | $return = []; |
||
76 | |||
77 | // Get popup output |
||
78 | $return[] = $this->getOutputHTML('popup'); |
||
79 | |||
80 | // Get embed codes |
||
81 | foreach ($this->config as $configKey => $configValue) { |
||
82 | if (!is_array($configValue) || !$configValue['enabled'] || !$this->isAllowed($configKey)) { |
||
83 | continue; |
||
84 | } |
||
85 | $return[] = $this->getOutputHTML('/cookies/'.$configKey.'/output'); |
||
86 | } |
||
87 | |||
88 | return implode("\n", $return); |
||
89 | } |
||
90 | |||
91 | public function getOutputHTML($filename) |
||
92 | { |
||
93 | if (!file_exists(__DIR__.'/output/'.$filename.'.php')) { |
||
94 | return false; |
||
95 | } |
||
96 | |||
97 | ob_start(); |
||
98 | include __DIR__.'/output/'.$filename.'.php'; |
||
99 | return trim(ob_get_clean()); |
||
100 | } |
||
101 | |||
102 | public function enabledCookies() |
||
103 | { |
||
104 | $return = []; |
||
105 | foreach ($this->config as $name => $value) { |
||
106 | if (!$this->isEnabled($name)) { |
||
107 | continue; |
||
108 | } |
||
109 | $return[$name] = $value['label']; |
||
110 | } |
||
111 | return $return; |
||
112 | } |
||
113 | |||
114 | public function disabledCookies() |
||
115 | { |
||
116 | $return = []; |
||
117 | foreach ($this->config as $name => $value) { |
||
118 | if (!$this->isEnabled($name) || !is_array($value) || $this->isAllowed($name)) { |
||
119 | continue; |
||
120 | } |
||
121 | $return[$name] = $value['label']; |
||
122 | } |
||
123 | return $return; |
||
124 | } |
||
125 | |||
126 | public static function setCookie( |
||
127 | $name, |
||
128 | $value, |
||
129 | $lifetime = 30, |
||
130 | $lifetimePeriod = 'days', |
||
131 | $domain = '/', |
||
132 | $secure = false |
||
133 | ) { |
||
134 | // Validate parameters |
||
135 | self::validateSetCookieParams($name, $value, $lifetime, $domain, $secure); |
||
136 | |||
137 | // Calculate expiry |
||
138 | $expiry = strtotime('+'.$lifetime.' '.$lifetimePeriod); |
||
139 | |||
140 | // Set cookie |
||
141 | return setcookie($name, $value, $expiry, $domain, $secure); |
||
142 | } |
||
143 | |||
144 | public static function validateSetCookieParams($name, $value, $lifetime, $domain, $secure) |
||
145 | { |
||
146 | // Types of parameters to check |
||
147 | $paramTypes = [ |
||
148 | // Type => Array of variables |
||
149 | 'string' => [$name, $value, $domain], |
||
150 | 'int' => [$lifetime], |
||
151 | 'bool' => [$secure], |
||
152 | ]; |
||
153 | |||
154 | // Validate basic parameters |
||
155 | $validParams = self::basicValidationChecks($paramTypes); |
||
156 | |||
157 | // Ensure parameters are still valid |
||
158 | if (!$validParams) { |
||
159 | // Failed parameter check |
||
160 | header('HTTP/1.0 403 Forbidden'); |
||
161 | throw new \Exception("Incorrect parameter passed to Cookie::set"); |
||
162 | } |
||
163 | |||
164 | return true; |
||
165 | } |
||
166 | |||
167 | public static function basicValidationChecks($paramTypes) |
||
178 | } |
||
179 | |||
180 | public function clearCookieGroup($groupName) |
||
181 | { |
||
182 | if (!file_exists(__DIR__.'/output/cookies/'.$groupName.'/cookies.php')) { |
||
183 | return false; |
||
184 | } |
||
185 | $clearCookies = include __DIR__.'/output/cookies/'.$groupName.'/cookies.php'; |
||
186 | |||
187 | $defaults = [ |
||
188 | 'path' => '/', |
||
189 | 'domain' => $_SERVER['HTTP_HOST'], |
||
190 | ]; |
||
191 | |||
192 | if (isset($clearCookies['defaults'])) { |
||
193 | $defaults = array_merge($defaults, $clearCookies['defaults']); |
||
194 | unset($clearCookies['defaults']); |
||
195 | } |
||
196 | |||
197 | $return = []; |
||
198 | |||
199 | foreach ($clearCookies as $cookie) { |
||
200 | $cookie['path'] = isset($cookie['path']) ? $cookie['path'] : $defaults['path']; |
||
201 | $cookie['domain'] = isset($cookie['domain']) ? $cookie['domain'] : $defaults['domain']; |
||
202 | self::destroyCookie($cookie['name'], $cookie['path'], $cookie['domain']); |
||
203 | $return[] = $cookie; |
||
204 | } |
||
205 | |||
206 | return $return; |
||
207 | } |
||
208 | |||
209 | public static function getCookie($name) |
||
213 | } |
||
214 | |||
215 | public static function destroyCookie($name, $path = '', $domain = '') |
||
216 | { |
||
217 | // Set cookie expiration to 1 hour ago |
||
218 | return setcookie($name, '', time() - 3600, $path, $domain); |
||
219 | } |
||
220 | } |
||
221 |