| Total Complexity | 46 |
| Total Lines | 215 |
| 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 | $return = json_encode($value); |
||
| 37 | return $return; |
||
| 38 | } |
||
| 39 | |||
| 40 | public static function decrypt($value) |
||
| 45 | } |
||
| 46 | |||
| 47 | public function isAllowed($name) |
||
| 51 | } |
||
| 52 | |||
| 53 | public function isEnabled($name) |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getConfig($name, $attribute) |
||
| 60 | { |
||
| 61 | return isset($this->config[$name]) && isset($this->config[$name][$attribute]) |
||
| 62 | ? $this->config[$name][$attribute] |
||
| 63 | : false; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function output() |
||
| 69 | } |
||
| 70 | |||
| 71 | public function getOutput() |
||
| 72 | { |
||
| 73 | $return = []; |
||
| 74 | |||
| 75 | // Get popup output |
||
| 76 | $return[] = $this->getOutputHTML('popup'); |
||
| 77 | |||
| 78 | // Get embed codes |
||
| 79 | foreach ($this->config as $configKey => $configValue) { |
||
| 80 | if (!is_array($configValue)) { |
||
| 81 | continue; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($configValue['enabled'] && $this->isAllowed($configKey)) { |
||
| 85 | $return[] = $this->getOutputHTML('/cookies/'.$configKey.'/output'); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return implode("\n", $return); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getOutputHTML($filename) |
||
| 93 | { |
||
| 94 | if (!file_exists(__DIR__.'/output/'.$filename.'.php')) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | ob_start(); |
||
| 99 | include __DIR__.'/output/'.$filename.'.php'; |
||
| 100 | return trim(ob_get_clean()); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function enabledCookies() |
||
| 104 | { |
||
| 105 | $return = []; |
||
| 106 | foreach ($this->config as $name => $value) { |
||
| 107 | if (!$this->isEnabled($name)) { |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | $return[$name] = $value['label']; |
||
| 111 | } |
||
| 112 | return $return; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function disabledCookies() |
||
| 116 | { |
||
| 117 | $return = []; |
||
| 118 | foreach ($this->config as $name => $value) { |
||
| 119 | if (!$this->isEnabled($name) || !is_array($value) || $this->isAllowed($name)) { |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | $return[$name] = $value['label']; |
||
| 123 | } |
||
| 124 | return $return; |
||
| 125 | } |
||
| 126 | |||
| 127 | public static function setCookie( |
||
| 128 | $name, |
||
| 129 | $value, |
||
| 130 | $lifetime = 30, |
||
| 131 | $lifetimePeriod = 'days', |
||
| 132 | $domain = '/', |
||
| 133 | $secure = false |
||
| 134 | ) { |
||
| 135 | // Validate parameters |
||
| 136 | self::validateSetCookieParams($name, $value, $lifetime, $domain, $secure); |
||
| 137 | |||
| 138 | // Calculate expiry |
||
| 139 | $expiry = strtotime('+'.$lifetime.' '.$lifetimePeriod); |
||
| 140 | |||
| 141 | // Set cookie |
||
| 142 | return setcookie($name, $value, $expiry, $domain, $secure); |
||
| 143 | } |
||
| 144 | |||
| 145 | public static function validateSetCookieParams($name, $value, $lifetime, $domain, $secure) |
||
| 146 | { |
||
| 147 | // Types of parameters to check |
||
| 148 | $paramTypes = [ |
||
| 149 | // Type => Array of variables |
||
| 150 | 'string' => [$name, $value, $domain], |
||
| 151 | 'int' => [$lifetime], |
||
| 152 | 'bool' => [$secure], |
||
| 153 | ]; |
||
| 154 | |||
| 155 | // Validate basic parameters |
||
| 156 | $validParams = self::basicValidationChecks($paramTypes); |
||
| 157 | |||
| 158 | // Ensure parameters are still valid |
||
| 159 | if (!$validParams) { |
||
| 160 | // Failed parameter check |
||
| 161 | header('HTTP/1.0 403 Forbidden'); |
||
| 162 | throw new \Exception("Incorrect parameter passed to Cookie::set"); |
||
| 163 | } |
||
| 164 | |||
| 165 | return true; |
||
| 166 | } |
||
| 167 | |||
| 168 | public static function basicValidationChecks($paramTypes) |
||
| 179 | } |
||
| 180 | |||
| 181 | public function clearCookieGroup($groupName) |
||
| 182 | { |
||
| 183 | if (!file_exists(__DIR__.'/output/cookies/'.$groupName.'/cookies.php')) { |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | $clearCookies = include __DIR__.'/output/cookies/'.$groupName.'/cookies.php'; |
||
| 187 | |||
| 188 | $defaults = [ |
||
| 189 | 'path' => '/', |
||
| 190 | 'domain' => $_SERVER['HTTP_HOST'], |
||
| 191 | ]; |
||
| 192 | |||
| 193 | if (isset($clearCookies['defaults'])) { |
||
| 194 | $defaults = array_merge($defaults, $clearCookies['defaults']); |
||
| 195 | unset($clearCookies['defaults']); |
||
| 196 | } |
||
| 197 | |||
| 198 | $return = []; |
||
| 199 | |||
| 200 | foreach ($clearCookies as $cookie) { |
||
| 201 | $cookie['path'] = isset($cookie['path']) ? $cookie['path'] : $defaults['path']; |
||
| 202 | $cookie['domain'] = isset($cookie['domain']) ? $cookie['domain'] : $defaults['domain']; |
||
| 203 | self::destroyCookie($cookie['name'], $cookie['path'], $cookie['domain']); |
||
| 204 | $return[] = $cookie; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $return; |
||
| 208 | } |
||
| 209 | |||
| 210 | public static function getCookie($name) |
||
| 214 | } |
||
| 215 | |||
| 216 | public static function destroyCookie($name, $path = '', $domain = '') |
||
| 217 | { |
||
| 218 | // Set cookie expiration to 1 hour ago |
||
| 219 | return setcookie($name, '', time() - 3600, $path, $domain); |
||
| 220 | } |
||
| 221 | } |
||
| 222 |