| Total Complexity | 60 |
| Total Lines | 225 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like URLGenerator 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 URLGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class URLGenerator implements IURLGenerator { |
||
| 54 | /** @var IConfig */ |
||
| 55 | private $config; |
||
| 56 | /** @var ICacheFactory */ |
||
| 57 | private $cacheFactory; |
||
| 58 | /** @var IRequest */ |
||
| 59 | private $request; |
||
| 60 | /** @var Router */ |
||
| 61 | private $router; |
||
| 62 | /** @var null|string */ |
||
| 63 | private $baseUrl = null; |
||
| 64 | |||
| 65 | public function __construct(IConfig $config, |
||
| 66 | ICacheFactory $cacheFactory, |
||
| 67 | IRequest $request, |
||
| 68 | Router $router) { |
||
| 69 | $this->config = $config; |
||
| 70 | $this->cacheFactory = $cacheFactory; |
||
| 71 | $this->request = $request; |
||
| 72 | $this->router = $router; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates an url using a defined route |
||
| 77 | * |
||
| 78 | * @param string $routeName |
||
| 79 | * @param array $arguments args with param=>value, will be appended to the returned url |
||
| 80 | * @return string the url |
||
| 81 | * |
||
| 82 | * Returns a url to the given route. |
||
| 83 | */ |
||
| 84 | public function linkToRoute(string $routeName, array $arguments = []): string { |
||
| 85 | return $this->router->generate($routeName, $arguments); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Creates an absolute url using a defined route |
||
| 90 | * @param string $routeName |
||
| 91 | * @param array $arguments args with param=>value, will be appended to the returned url |
||
| 92 | * @return string the url |
||
| 93 | * |
||
| 94 | * Returns an absolute url to the given route. |
||
| 95 | */ |
||
| 96 | public function linkToRouteAbsolute(string $routeName, array $arguments = []): string { |
||
| 98 | } |
||
| 99 | |||
| 100 | public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string { |
||
| 101 | $route = $this->router->generate('ocs.'.$routeName, $arguments, false); |
||
| 102 | |||
| 103 | $indexPhpPos = strpos($route, '/index.php/'); |
||
| 104 | if ($indexPhpPos !== false) { |
||
| 105 | $route = substr($route, $indexPhpPos + 10); |
||
| 106 | } |
||
| 107 | |||
| 108 | $route = substr($route, 7); |
||
| 109 | $route = '/ocs/v2.php' . $route; |
||
| 110 | |||
| 111 | return $this->getAbsoluteURL($route); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Creates an url |
||
| 116 | * |
||
| 117 | * @param string $appName app |
||
| 118 | * @param string $file file |
||
| 119 | * @param array $args array with param=>value, will be appended to the returned url |
||
| 120 | * The value of $args will be urlencoded |
||
| 121 | * @return string the url |
||
| 122 | * |
||
| 123 | * Returns a url to the given app and file. |
||
| 124 | */ |
||
| 125 | public function linkTo(string $appName, string $file, array $args = []): string { |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Creates path to an image |
||
| 165 | * |
||
| 166 | * @param string $appName app |
||
| 167 | * @param string $file image name |
||
| 168 | * @throws \RuntimeException If the image does not exist |
||
| 169 | * @return string the url |
||
| 170 | * |
||
| 171 | * Returns the path to the image. |
||
| 172 | */ |
||
| 173 | public function imagePath(string $appName, string $file): string { |
||
| 174 | $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-'); |
||
| 175 | $cacheKey = $appName.'-'.$file; |
||
| 176 | if ($key = $cache->get($cacheKey)) { |
||
| 177 | return $key; |
||
| 178 | } |
||
| 179 | |||
| 180 | // Read the selected theme from the config file |
||
| 181 | $theme = \OC_Util::getTheme(); |
||
| 182 | |||
| 183 | //if a theme has a png but not an svg always use the png |
||
| 184 | $basename = substr(basename($file),0,-4); |
||
| 185 | |||
| 186 | $appPath = \OC_App::getAppPath($appName); |
||
| 187 | |||
| 188 | // Check if the app is in the app folder |
||
| 189 | $path = ''; |
||
| 190 | $themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming'); |
||
| 191 | $themingImagePath = false; |
||
| 192 | if ($themingEnabled) { |
||
| 193 | $themingDefaults = \OC::$server->getThemingDefaults(); |
||
| 194 | if ($themingDefaults instanceof ThemingDefaults) { |
||
| 195 | $themingImagePath = $themingDefaults->replaceImagePath($appName, $file); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$file")) { |
||
| 200 | $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$file"; |
||
| 201 | } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.svg") |
||
| 202 | && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.png")) { |
||
| 203 | $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$basename.png"; |
||
| 204 | } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$file")) { |
||
| 205 | $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$file"; |
||
| 206 | } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.svg") |
||
| 207 | && file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.png"))) { |
||
| 208 | $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$basename.png"; |
||
| 209 | } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$file")) { |
||
| 210 | $path = \OC::$WEBROOT . "/themes/$theme/core/img/$file"; |
||
| 211 | } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg") |
||
| 212 | && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) { |
||
| 213 | $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; |
||
| 214 | } elseif ($themingEnabled && $themingImagePath) { |
||
| 215 | $path = $themingImagePath; |
||
| 216 | } elseif ($appPath && file_exists($appPath . "/img/$file")) { |
||
| 217 | $path = \OC_App::getAppWebPath($appName) . "/img/$file"; |
||
| 218 | } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg") |
||
| 219 | && file_exists($appPath . "/img/$basename.png")) { |
||
| 220 | $path = \OC_App::getAppWebPath($appName) . "/img/$basename.png"; |
||
| 221 | } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/$appName/img/$file")) { |
||
| 222 | $path = \OC::$WEBROOT . "/$appName/img/$file"; |
||
| 223 | } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.svg") |
||
| 224 | && file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.png"))) { |
||
| 225 | $path = \OC::$WEBROOT . "/$appName/img/$basename.png"; |
||
| 226 | } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$file")) { |
||
| 227 | $path = \OC::$WEBROOT . "/core/img/$file"; |
||
| 228 | } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg") |
||
| 229 | && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) { |
||
| 230 | $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($path !== '') { |
||
| 234 | $cache->set($cacheKey, $path); |
||
| 235 | return $path; |
||
| 236 | } |
||
| 237 | |||
| 238 | throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT); |
||
| 239 | } |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Makes an URL absolute |
||
| 244 | * @param string $url the url in the ownCloud host |
||
| 245 | * @return string the absolute version of the url |
||
| 246 | */ |
||
| 247 | public function getAbsoluteURL(string $url): string { |
||
| 248 | $separator = strpos($url, '/') === 0 ? '' : '/'; |
||
| 249 | |||
| 250 | if (\OC::$CLI && !\defined('PHPUNIT_RUN')) { |
||
| 251 | return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/'); |
||
| 252 | } |
||
| 253 | // The ownCloud web root can already be prepended. |
||
| 254 | if (\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) { |
||
| 255 | $url = substr($url, \strlen(\OC::$WEBROOT)); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $this->getBaseUrl() . $separator . $url; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $key |
||
| 263 | * @return string url to the online documentation |
||
| 264 | */ |
||
| 265 | public function linkToDocs(string $key): string { |
||
| 266 | $theme = \OC::$server->getThemingDefaults(); |
||
| 267 | return $theme->buildDocLinkToKey($key); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return string base url of the current request |
||
| 272 | */ |
||
| 273 | public function getBaseUrl(): string { |
||
| 278 | } |
||
| 279 | } |
||
| 280 |