Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 42 | class TemplatesController extends Controller { |
||
| 43 | /** @var IL10N */ |
||
| 44 | private $l10n; |
||
| 45 | |||
| 46 | /** @var TemplateManager */ |
||
| 47 | private $manager; |
||
| 48 | |||
| 49 | /** @var IPreview */ |
||
| 50 | private $preview; |
||
| 51 | |||
| 52 | /** @var int Max template size */ |
||
| 53 | private $maxSize = 20 * 1024 * 1024; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Templates controller |
||
| 57 | * |
||
| 58 | * @param string $appName |
||
| 59 | * @param IRequest $request |
||
| 60 | * @param L10N $l10n |
||
| 61 | * @param TemplateManager $manager |
||
| 62 | * @param IPreview $preview |
||
| 63 | */ |
||
| 64 | public function __construct($appName, |
||
| 65 | IRequest $request, |
||
| 66 | IL10N $l10n, |
||
| 67 | TemplateManager $manager, |
||
| 68 | IPreview $preview) { |
||
| 69 | parent::__construct($appName, $request); |
||
| 70 | |||
| 71 | $this->appName = $appName; |
||
| 72 | $this->request = $request; |
||
| 73 | $this->l10n = $l10n; |
||
| 74 | $this->manager = $manager; |
||
| 75 | $this->preview = $preview; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @NoAdminRequired |
||
| 80 | * @NoCSRFRequired |
||
| 81 | * |
||
| 82 | * Get preview for a specific template |
||
| 83 | * |
||
| 84 | * @param int $fileId The template id |
||
| 85 | * @param int $x |
||
| 86 | * @param int $y |
||
| 87 | * @param bool $a |
||
| 88 | * @param bool $forceIcon |
||
| 89 | * @param string $mode |
||
| 90 | * @return DataResponse |
||
| 91 | * @throws NotFoundResponse |
||
| 92 | */ |
||
| 93 | public function getPreview($fileId, |
||
| 94 | $x = 150, |
||
| 95 | $y = 150, |
||
| 96 | $a = false, |
||
| 97 | $forceIcon = true, |
||
| 98 | $mode = 'fill') { |
||
| 99 | |||
| 100 | if ($fileId === '' || $x === 0 || $y === 0) { |
||
|
|
|||
| 101 | return new DataResponse([], Http::STATUS_BAD_REQUEST); |
||
| 102 | } |
||
| 103 | |||
| 104 | try { |
||
| 105 | $template = $this->manager->get($fileId); |
||
| 106 | } catch (NotFoundException $e) { |
||
| 107 | return new DataResponse([], Http::STATUS_NOT_FOUND); |
||
| 108 | } |
||
| 109 | |||
| 110 | if ($template instanceof ISimpleFile) { |
||
| 111 | return new DataResponse([], Http::STATUS_NOT_FOUND); |
||
| 112 | } |
||
| 113 | |||
| 114 | return $this->fetchPreview($template, $x, $y, $a, $forceIcon, $mode); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Add a global template |
||
| 119 | * |
||
| 120 | * @return JSONResponse |
||
| 121 | */ |
||
| 122 | public function add() { |
||
| 123 | $files = $this->request->getUploadedFile('files'); |
||
| 124 | |||
| 125 | if (!is_null($files)) { |
||
| 126 | if ($files['error'][0] === 0 |
||
| 127 | && is_uploaded_file($files['tmp_name'][0]) |
||
| 128 | && !Filesystem::isFileBlacklisted($files['tmp_name'][0])) { |
||
| 129 | |||
| 130 | // TODO: ensure the size limit is decent for preview |
||
| 131 | View Code Duplication | if ($files['size'][0] > $this->maxSize) { |
|
| 132 | return new JSONResponse( |
||
| 133 | ['data' => ['message' => $this->l10n->t('File is too big')]], |
||
| 134 | Http::STATUS_BAD_REQUEST |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | View Code Duplication | if (!$this->manager->isValidTemplateMime($files['type'][0])) { |
|
| 139 | return new JSONResponse( |
||
| 140 | ['data' => ['message' => $this->l10n->t('Only template files can be uploaded')]], |
||
| 141 | Http::STATUS_BAD_REQUEST |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | $templateName = $files['name'][0]; |
||
| 146 | $templateFile = file_get_contents($files['tmp_name'][0]); |
||
| 147 | |||
| 148 | unlink($files['tmp_name'][0]); |
||
| 149 | |||
| 150 | $template = $this->manager->add($templateName, $templateFile); |
||
| 151 | |||
| 152 | return new JSONResponse( |
||
| 153 | ['data' => $template], |
||
| 154 | Http::STATUS_CREATED |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return new JSONResponse( |
||
| 160 | ['data' => ['message' => $this->l10n->t('Invalid file provided')]], |
||
| 161 | Http::STATUS_BAD_REQUEST |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Delete a global template |
||
| 167 | * |
||
| 168 | * @param int $fileId |
||
| 169 | * @return JSONResponse |
||
| 170 | */ |
||
| 171 | public function delete($fileId) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param Node $node |
||
| 189 | * @param int $x |
||
| 190 | * @param int $y |
||
| 191 | * @param bool $a |
||
| 192 | * @param bool $forceIcon |
||
| 193 | * @param string $mode |
||
| 194 | * @return DataResponse|FileDisplayResponse |
||
| 195 | */ |
||
| 196 | private function fetchPreview( |
||
| 223 | } |
||
| 224 |