| Conditions | 11 |
| Paths | 16 |
| Total Lines | 54 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 23 | public function run(RequestInterface $request, ResponseInterface $response) |
||
| 24 | { |
||
| 25 | $translator = $this->translator(); |
||
| 26 | |||
| 27 | $cacheType = $request->getParam('cache_type'); |
||
|
|
|||
| 28 | if (!is_string($cacheType) || empty($cacheType)) { |
||
| 29 | $this->addFeedback('error', $translator->translate('Cache type not defined.')); |
||
| 30 | $this->setSuccess(false); |
||
| 31 | return $response->withStatus(400); |
||
| 32 | } |
||
| 33 | |||
| 34 | $result = false; |
||
| 35 | $message = null; |
||
| 36 | |||
| 37 | if ($cacheType === 'global') { |
||
| 38 | $result = $this->clearGlobalCache(); |
||
| 39 | |||
| 40 | if ($result) { |
||
| 41 | $message = $translator->translate('Cache cleared successfully.'); |
||
| 42 | } else { |
||
| 43 | $message = $translator->translate('Failed to clear cache.'); |
||
| 44 | } |
||
| 45 | } elseif ($cacheType === 'pages') { |
||
| 46 | $result = $this->clearPagesCache(); |
||
| 47 | |||
| 48 | if ($result) { |
||
| 49 | $message = $translator->translate('Pages cache cleared successfully.'); |
||
| 50 | } else { |
||
| 51 | $message = $translator->translate('Failed to clear pages cache.'); |
||
| 52 | } |
||
| 53 | } elseif ($cacheType === 'objects') { |
||
| 54 | $result = $this->clearObjectsCache(); |
||
| 55 | |||
| 56 | if ($result) { |
||
| 57 | $message = $translator->translate('Objects cache cleared successfully.'); |
||
| 58 | } else { |
||
| 59 | $message = $translator->translate('Failed to clear objects cache.'); |
||
| 60 | } |
||
| 61 | } elseif ($cacheType === 'item') { |
||
| 62 | $message = $translator->translate('Deleting cache items is unsupported, for now.'); |
||
| 63 | } else { |
||
| 64 | $this->addFeedback('error', $translator->translate(sprintf('Invalid cache type "%s"', $cacheType))); |
||
| 65 | $this->setSuccess(false); |
||
| 66 | return $response->withStatus(400); |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->setSuccess($result); |
||
| 70 | |||
| 71 | if ($result) { |
||
| 72 | $this->addFeedback('success', $message); |
||
| 73 | return $response; |
||
| 74 | } else { |
||
| 75 | $this->addFeedback('error', $message); |
||
| 76 | return $response->withStatus(500); |
||
| 77 | } |
||
| 116 |