Conditions | 8 |
Paths | 47 |
Total Lines | 53 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
77 | public function certificateFileResponse( |
||
78 | Certificate $certificate, |
||
79 | bool $add_watermark = false, |
||
80 | Watermark $watermark = null |
||
81 | ): ResponseInterface { |
||
82 | $filesystem = $this->filesystem_service->filesystem($certificate->tree()); |
||
83 | $filename = $certificate->path(); |
||
84 | |||
85 | if (!$add_watermark) { |
||
86 | return $this->fileResponse($filesystem, $filename, false); |
||
87 | } |
||
88 | |||
89 | try { |
||
90 | $image = $this->imageManager()->make($filesystem->readStream($filename)); |
||
91 | $image = $this->autorotateImage($image); |
||
92 | |||
93 | if ($watermark == null) { |
||
94 | throw new InvalidArgumentException('Watermark data not defined'); |
||
95 | } |
||
96 | |||
97 | $width = $image->width(); |
||
98 | $height = $image->height(); |
||
99 | |||
100 | $watermark->adjustSize($width); |
||
101 | $watermark_x = (int) ceil($watermark->textLengthEstimate() * 1.5); |
||
102 | $watermark_y = $watermark->size() * 12 + 1; |
||
103 | |||
104 | $font_definition = function (AbstractFont $font) use ($watermark): void { |
||
105 | $font->file(Webtrees::ROOT_DIR . 'resources/fonts/DejaVuSans.ttf'); |
||
106 | $font->color($watermark->color()); |
||
107 | $font->size($watermark->size()); |
||
108 | $font->valign('top'); |
||
109 | }; |
||
110 | |||
111 | for ($i = min((int) ceil($width * 0.1), $watermark_x); $i < $width; $i += $watermark_x) { |
||
112 | for ($j = min((int) ceil($height * 0.1), $watermark_y); $j < $height; $j += $watermark_y) { |
||
113 | $image = $image->text($watermark->text(), $i, $j, $font_definition); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; |
||
118 | $quality = $this->extractImageQuality($image, static::GD_DEFAULT_IMAGE_QUALITY); |
||
119 | $data = (string) $image->encode($format, $quality); |
||
120 | |||
121 | return $this->imageResponse($data, $image->mime(), ''); |
||
122 | } catch (NotReadableException $ex) { |
||
123 | return $this->replacementImageResponse(pathinfo($filename, PATHINFO_EXTENSION)) |
||
|
|||
124 | ->withHeader('X-Image-Exception', $ex->getMessage()); |
||
125 | } catch (FilesystemException | UnableToReadFile $ex) { |
||
126 | return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); |
||
127 | } catch (Throwable $ex) { |
||
128 | return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) |
||
129 | ->withHeader('X-Image-Exception', $ex->getMessage()); |
||
130 | } |
||
202 |