Total Complexity | 43 |
Total Lines | 189 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like FileDumpController 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 FileDumpController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class FileDumpController |
||
39 | { |
||
40 | /** |
||
41 | * @var ResourceFactory |
||
42 | */ |
||
43 | protected $resourceFactory; |
||
44 | |||
45 | public function __construct(ResourceFactory $resourceFactory) |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Main method to dump a file |
||
52 | * |
||
53 | * @param ServerRequestInterface $request |
||
54 | * @return ResponseInterface |
||
55 | * @throws \InvalidArgumentException |
||
56 | * @throws \RuntimeException |
||
57 | * @throws FileDoesNotExistException |
||
58 | * @throws \UnexpectedValueException |
||
59 | */ |
||
60 | public function dumpAction(ServerRequestInterface $request): ResponseInterface |
||
61 | { |
||
62 | $parameters = $this->buildParametersFromRequest($request); |
||
63 | |||
64 | if (!$this->isTokenValid($parameters, $request)) { |
||
65 | return (new Response())->withStatus(403); |
||
66 | } |
||
67 | $file = $this->createFileObjectByParameters($parameters); |
||
68 | if ($file === null) { |
||
69 | return (new Response())->withStatus(404); |
||
70 | } |
||
71 | |||
72 | // Hook: allow some other process to do some security/access checks. Hook should return 403 response if access is rejected, void otherwise |
||
73 | foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['FileDumpEID.php']['checkFileAccess'] ?? [] as $className) { |
||
74 | $hookObject = GeneralUtility::makeInstance($className); |
||
75 | if (!$hookObject instanceof FileDumpEIDHookInterface) { |
||
76 | throw new \UnexpectedValueException($className . ' must implement interface ' . FileDumpEIDHookInterface::class, 1394442417); |
||
77 | } |
||
78 | $response = $hookObject->checkFileAccess($file); |
||
79 | if ($response instanceof ResponseInterface) { |
||
80 | return $response; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | $processingInstructions = []; |
||
85 | |||
86 | // Apply cropping, if possible |
||
87 | if (!empty($parameters['cv'])) { |
||
88 | $cropVariant = $parameters['cv']; |
||
89 | $cropString = $file instanceof FileReference ? $file->getProperty('crop') : ''; |
||
90 | $cropArea = CropVariantCollection::create((string)$cropString)->getCropArea($cropVariant); |
||
91 | $processingInstructions = array_merge( |
||
92 | $processingInstructions, |
||
93 | [ |
||
94 | 'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($file), |
||
95 | ] |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | // Apply width/height, if given |
||
100 | if (!empty($parameters['s'])) { |
||
101 | $size = GeneralUtility::trimExplode(':', $parameters['s']); |
||
102 | $processingInstructions = array_merge( |
||
103 | $processingInstructions, |
||
104 | [ |
||
105 | 'width' => $size[0] ?? null, |
||
106 | 'height' => $size[1] ?? null, |
||
107 | 'minWidth' => $size[2] ? (int)$size[2] : null, |
||
108 | 'minHeight' => $size[3] ? (int)$size[3] : null, |
||
109 | 'maxWidth' => $size[4] ? (int)$size[4] : null, |
||
110 | 'maxHeight' => $size[5] ? (int)$size[5] : null |
||
111 | ] |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | if (!empty($processingInstructions) && !($file instanceof ProcessedFile)) { |
||
116 | if (is_callable([$file, 'getOriginalFile'])) { |
||
117 | // Get the original file from the file reference |
||
118 | $file = $file->getOriginalFile(); |
||
|
|||
119 | } |
||
120 | $file = $file->process(ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions); |
||
121 | } |
||
122 | |||
123 | return $file->getStorage()->streamFile($file); |
||
124 | } |
||
125 | |||
126 | protected function buildParametersFromRequest(ServerRequestInterface $request): array |
||
127 | { |
||
128 | $parameters = ['eID' => 'dumpFile']; |
||
129 | $queryParams = $request->getQueryParams(); |
||
130 | // Identifier of what to process. f, r or p |
||
131 | // Only needed while hash_equals |
||
132 | $t = (string)($queryParams['t'] ?? ''); |
||
133 | if ($t) { |
||
134 | $parameters['t'] = $t; |
||
135 | } |
||
136 | // sys_file |
||
137 | $f = (string)($queryParams['f'] ?? ''); |
||
138 | if ($f) { |
||
139 | $parameters['f'] = (int)$f; |
||
140 | } |
||
141 | // sys_file_reference |
||
142 | $r = (string)($queryParams['r'] ?? ''); |
||
143 | if ($r) { |
||
144 | $parameters['r'] = (int)$r; |
||
145 | } |
||
146 | // Processed file |
||
147 | $p = (string)($queryParams['p'] ?? ''); |
||
148 | if ($p) { |
||
149 | $parameters['p'] = (int)$p; |
||
150 | } |
||
151 | // File's width and height in this order: w:h:minW:minH:maxW:maxH |
||
152 | $s = (string)($queryParams['s'] ?? ''); |
||
153 | if ($s) { |
||
154 | $parameters['s'] = $s; |
||
155 | } |
||
156 | // File's crop variant |
||
157 | $v = (string)($queryParams['cv'] ?? ''); |
||
158 | if ($v) { |
||
159 | $parameters['cv'] = (string)$v; |
||
160 | } |
||
161 | |||
162 | return $parameters; |
||
163 | } |
||
164 | |||
165 | protected function isTokenValid(array $parameters, ServerRequestInterface $request): bool |
||
166 | { |
||
167 | return hash_equals( |
||
168 | GeneralUtility::hmac(implode('|', $parameters), 'resourceStorageDumpFile'), |
||
169 | $request->getQueryParams()['token'] ?? '' |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param array $parameters |
||
175 | * @return File|FileReference|ProcessedFile|null |
||
176 | */ |
||
177 | protected function createFileObjectByParameters(array $parameters) |
||
220 | } |
||
221 | |||
222 | protected function isFileValid(FileInterface $file): bool |
||
227 | } |
||
228 | } |
||
229 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.