Total Complexity | 42 |
Total Lines | 255 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Complex classes like Util 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 Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Util { |
||
41 | |||
42 | private IConfig $config; |
||
43 | private IAppManager $appManager; |
||
44 | private IAppData $appData; |
||
45 | private ImageManager $imageManager; |
||
46 | |||
47 | public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData, ImageManager $imageManager) { |
||
48 | $this->config = $config; |
||
49 | $this->appManager = $appManager; |
||
50 | $this->appData = $appData; |
||
51 | $this->imageManager = $imageManager; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Should we invert the text on this background color? |
||
56 | * @param string $color rgb color value |
||
57 | * @return bool |
||
58 | */ |
||
59 | public function invertTextColor(string $color): bool { |
||
60 | return $this->isBrightColor($color); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Is this color too bright ? |
||
65 | * @param string $color rgb color value |
||
66 | * @return bool |
||
67 | */ |
||
68 | public function isBrightColor(string $color): bool { |
||
69 | $l = $this->calculateLuma($color); |
||
70 | if ($l > 0.6) { |
||
71 | return true; |
||
72 | } else { |
||
73 | return false; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * get color for on-page elements: |
||
79 | * theme color by default, grey if theme color is to bright |
||
80 | * @param string $color |
||
81 | * @param bool $brightBackground |
||
82 | * @return string |
||
83 | */ |
||
84 | public function elementColor($color, bool $brightBackground = true) { |
||
98 | } |
||
99 | |||
100 | public function mix(string $color1, string $color2, int $factor): string { |
||
101 | $color = new Color($color1); |
||
102 | return '#' . $color->mix($color2, $factor); |
||
103 | } |
||
104 | |||
105 | public function lighten(string $color, int $factor): string { |
||
106 | $color = new Color($color); |
||
107 | return '#' . $color->lighten($factor); |
||
108 | } |
||
109 | |||
110 | public function darken(string $color, int $factor): string { |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Convert RGB to HSL |
||
117 | * |
||
118 | * Copied from cssphp, copyright Leaf Corcoran, licensed under MIT |
||
119 | * |
||
120 | * @param int $red |
||
121 | * @param int $green |
||
122 | * @param int $blue |
||
123 | * |
||
124 | * @return float[] |
||
125 | */ |
||
126 | public function toHSL(int $red, int $green, int $blue): array { |
||
127 | $color = new Color(Color::rgbToHex(['R' => $red, 'G' => $green, 'B' => $blue])); |
||
128 | return array_values($color->getHsl()); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param string $color rgb color value |
||
133 | * @return float |
||
134 | */ |
||
135 | public function calculateLuminance(string $color): float { |
||
136 | [$red, $green, $blue] = $this->hexToRGB($color); |
||
137 | $hsl = $this->toHSL($red, $green, $blue); |
||
138 | return $hsl[2]; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param string $color rgb color value |
||
143 | * @return float |
||
144 | */ |
||
145 | public function calculateLuma(string $color): float { |
||
146 | [$red, $green, $blue] = $this->hexToRGB($color); |
||
147 | return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param string $color rgb color value |
||
152 | * @return int[] |
||
153 | * @psalm-return array{0: int, 1: int, 2: int} |
||
154 | */ |
||
155 | public function hexToRGB(string $color): array { |
||
156 | $color = new Color($color); |
||
157 | return array_values($color->getRgb()); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param $color |
||
162 | * @return string base64 encoded radio button svg |
||
163 | */ |
||
164 | public function generateRadioButton($color) { |
||
165 | $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' . |
||
166 | '<path d="M8 1a7 7 0 0 0-7 7 7 7 0 0 0 7 7 7 7 0 0 0 7-7 7 7 0 0 0-7-7zm0 1a6 6 0 0 1 6 6 6 6 0 0 1-6 6 6 6 0 0 1-6-6 6 6 0 0 1 6-6zm0 2a4 4 0 1 0 0 8 4 4 0 0 0 0-8z" fill="'.$color.'"/></svg>'; |
||
167 | return base64_encode($radioButtonIcon); |
||
168 | } |
||
169 | |||
170 | |||
171 | /** |
||
172 | * @param $app string app name |
||
173 | * @return string|ISimpleFile path to app icon / file of logo |
||
174 | */ |
||
175 | public function getAppIcon($app) { |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @param $app string app name |
||
203 | * @param $image string relative path to image in app folder |
||
204 | * @return string|false absolute path to image |
||
205 | */ |
||
206 | public function getAppImage($app, $image) { |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * replace default color with a custom one |
||
248 | * |
||
249 | * @param $svg string content of a svg file |
||
250 | * @param $color string color to match |
||
251 | * @return string |
||
252 | */ |
||
253 | public function colorizeSvg($svg, $color) { |
||
254 | $svg = preg_replace('/#0082c9/i', $color, $svg); |
||
255 | return $svg; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Check if a custom theme is set in the server configuration |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function isAlreadyThemed() { |
||
264 | $theme = $this->config->getSystemValue('theme', ''); |
||
265 | if ($theme !== '') { |
||
266 | return true; |
||
267 | } |
||
268 | return false; |
||
269 | } |
||
270 | |||
271 | public function isBackgroundThemed() { |
||
272 | $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', ''); |
||
273 | return $backgroundLogo !== '' && $backgroundLogo !== 'backgroundColor'; |
||
274 | } |
||
275 | |||
276 | public function isLogoThemed() { |
||
279 | } |
||
280 | |||
281 | public function getCacheBuster(): string { |
||
282 | $userSession = \OC::$server->get(IUserSession::class); |
||
283 | $userId = ''; |
||
284 | $user = $userSession->getUser(); |
||
285 | if (!is_null($user)) { |
||
286 | $userId = $user->getUID(); |
||
287 | } |
||
288 | $userCacheBuster = ''; |
||
295 | } |
||
296 | } |
||
297 |