Complex classes like FileManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FileManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class FileManager |
||
33 | { |
||
34 | /** |
||
35 | * @var Filesystem |
||
36 | */ |
||
37 | private $fileSystem; |
||
38 | |||
39 | /** |
||
40 | * @var DocumentManager |
||
41 | */ |
||
42 | private $documentManager; |
||
43 | |||
44 | /** @var array allowedMimeTypes Control files to be saved and returned */ |
||
45 | private $allowedMimeTypes = []; |
||
46 | |||
47 | /** |
||
48 | * FileManager constructor. |
||
49 | * |
||
50 | * @param Filesystem $fileSystem file system abstraction layer for s3 and more |
||
51 | * @param ManagerRegistry $managerRegistry MongoDB registry manager |
||
52 | */ |
||
53 | public function __construct( |
||
60 | |||
61 | /** |
||
62 | * Configure allowed content types, empty is equal to all |
||
63 | * |
||
64 | * @param array $mimeTypes of Allowed types, application/pdf, image/jpeg... |
||
65 | * |
||
66 | * @return void |
||
67 | */ |
||
68 | public function setAllowedMimeTypes(array $mimeTypes) |
||
72 | |||
73 | /** |
||
74 | * Will update the response object with provided file data |
||
75 | * |
||
76 | * @param Response $response To building the response on |
||
77 | * @param DocumentFile $file File document object from DB |
||
78 | * |
||
79 | * @return Response |
||
80 | * @throws InvalidArgumentException if invalid info fetched from fileSystem |
||
81 | */ |
||
82 | public function buildGetContentResponse(Response $response, FileDocument $file) |
||
83 | { |
||
84 | /** @var FileMetadataBase $metadata */ |
||
85 | $metadata = $file->getMetadata(); |
||
86 | if (!$metadata) { |
||
87 | throw new InvalidArgumentException('Loaded file have no valid metadata'); |
||
88 | } |
||
89 | |||
90 | // We use file's mimeType, just in case none we use DB's. |
||
91 | $mimeType = $this->fileSystem->mimeType($file->getId()); |
||
92 | if (!$mimeType) { |
||
93 | $mimeType = $metadata->getMime(); |
||
94 | } |
||
95 | if ($this->allowedMimeTypes && !in_array($mimeType, $this->allowedMimeTypes)) { |
||
96 | throw new InvalidArgumentException('File mime type: '.$mimeType.' is not allowed as response.'); |
||
97 | } |
||
98 | |||
99 | // Read Data |
||
100 | $file = $this->fileSystem->read($file->getId()); |
||
101 | |||
102 | // Create Response |
||
103 | $disposition = $response->headers->makeDisposition( |
||
104 | ResponseHeaderBag::DISPOSITION_INLINE, |
||
105 | $metadata->getFilename() |
||
106 | ); |
||
107 | $response |
||
108 | ->setStatusCode(Response::HTTP_OK) |
||
109 | ->setContent($file); |
||
110 | $response |
||
111 | ->headers->set('Content-Type', $mimeType); |
||
112 | $response |
||
113 | ->headers->set('Content-Disposition', $disposition); |
||
114 | return $response; |
||
115 | } |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Save or update a file |
||
120 | * |
||
121 | * @param string $id ID of file |
||
122 | * @param String $data content to save |
||
123 | * |
||
124 | * @return GaufretteFile |
||
125 | * |
||
126 | * @throws BadRequestHttpException |
||
127 | */ |
||
128 | public function saveFile($id, $data) |
||
138 | |||
139 | /** |
||
140 | * @param DocumentFile $document File Document |
||
141 | * @param Request $request Request bag |
||
142 | * @param DocumentModel $model File Document Model |
||
143 | * @return DocumentFile |
||
144 | */ |
||
145 | public function handleSaveRequest( |
||
195 | |||
196 | /** |
||
197 | * Create the basic needs for a file |
||
198 | * |
||
199 | * @param DocumentFile $document Post or Put file document |
||
200 | * @param UploadedFile $file To be used in set metadata |
||
201 | * @param DocumentFile $original If there is a original document |
||
202 | * |
||
203 | * @return DocumentFile |
||
204 | * @throws InvalidArgumentException |
||
205 | */ |
||
206 | private function buildFileDocument(FileDocument $document, $file, $original) |
||
248 | |||
249 | /** |
||
250 | * Simple validation for post/put request |
||
251 | * |
||
252 | * @param DocumentFile $document File document |
||
253 | * @param string $requestId Request ID |
||
254 | * @return bool |
||
255 | */ |
||
256 | private function validIdRequest(FileDocument $document, $requestId) |
||
266 | |||
267 | /** |
||
268 | * Simple delete item from file system |
||
269 | * |
||
270 | * @param string $id ID of file to be deleted |
||
271 | * |
||
272 | * @return void |
||
273 | */ |
||
274 | public function remove($id) |
||
280 | |||
281 | /** |
||
282 | * Set global uploaded file. |
||
283 | * Only ONE file allowed per upload. |
||
284 | * |
||
285 | * @param Request $request service request |
||
286 | * @return UploadedFile if file was uploaded |
||
287 | * @throws InvalidArgumentException |
||
288 | */ |
||
289 | private function getUploadedFileFromRequest(Request $request) |
||
306 | } |
||
307 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.