Conditions | 12 |
Paths | 50 |
Total Lines | 73 |
Code Lines | 48 |
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 |
||
74 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
75 | { |
||
76 | $tree = $request->getAttribute('tree'); |
||
77 | assert($tree instanceof Tree); |
||
78 | |||
79 | $params = $request->getParsedBody(); |
||
80 | $body = $params['body']; |
||
81 | $from_email = $params['from_email']; |
||
82 | $from_name = $params['from_name']; |
||
83 | $subject = $params['subject']; |
||
84 | $to = $params['to']; |
||
85 | $url = $params['url']; |
||
86 | $ip = $request->getAttribute('client-ip'); |
||
87 | $to_user = $this->user_service->findByUserName($to); |
||
88 | |||
89 | if ($to_user === null) { |
||
90 | throw new NotFoundHttpException(); |
||
91 | } |
||
92 | |||
93 | if (!in_array($to_user, $this->message_service->validContacts($tree), false)) { |
||
94 | throw new AccessDeniedHttpException('Invalid contact user id'); |
||
95 | } |
||
96 | |||
97 | $errors = $body === '' || $subject === '' || $from_email === '' || $from_name === ''; |
||
98 | |||
99 | if (!preg_match('/^[^@]+@([^@]+)$/', $from_email, $match) || !checkdnsrr($match[1])) { |
||
100 | FlashMessages::addMessage(I18N::translate('Please enter a valid email address.'), 'danger'); |
||
101 | $errors = true; |
||
102 | } |
||
103 | |||
104 | $base_url = $request->getAttribute('base_url'); |
||
105 | |||
106 | if (preg_match('/(?!' . preg_quote($base_url, '/') . ')(((?:ftp|http|https):\/\/)[a-zA-Z0-9.-]+)/', $subject . $body, $match)) { |
||
107 | FlashMessages::addMessage(I18N::translate('You are not allowed to send messages that contain external links.') . ' ' . /* I18N: e.g. ‘You should delete the “http://” from “http://www.example.com” and try again.’ */ |
||
108 | I18N::translate('You should delete the “%1$s” from “%2$s” and try again.', $match[2], $match[1]), 'danger'); |
||
109 | $errors = true; |
||
110 | } |
||
111 | |||
112 | if ($errors) { |
||
113 | return redirect(route(ContactPage::class, [ |
||
114 | 'body' => $body, |
||
115 | 'from_email' => $from_email, |
||
116 | 'from_name' => $from_name, |
||
117 | 'subject' => $subject, |
||
118 | 'to' => $to, |
||
119 | 'tree' => $tree->name(), |
||
120 | 'url' => $url, |
||
121 | ])); |
||
122 | } |
||
123 | |||
124 | $sender = new GuestUser($from_email, $from_name); |
||
125 | |||
126 | if ($this->message_service->deliverMessage($sender, $to_user, $subject, $body, $url, $ip)) { |
||
127 | FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($to_user->realName())), 'success'); |
||
128 | |||
129 | $url = $url ?: route('tree-page', ['tree' => $tree->name()]); |
||
130 | |||
131 | return redirect($url); |
||
132 | } |
||
133 | |||
134 | FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger'); |
||
135 | |||
136 | $redirect_url = route(ContactPage::class, [ |
||
137 | 'body' => $body, |
||
138 | 'from_email' => $from_email, |
||
139 | 'from_name' => $from_name, |
||
140 | 'subject' => $subject, |
||
141 | 'to' => $to, |
||
142 | 'tree' => $tree->name(), |
||
143 | 'url' => $url, |
||
144 | ]); |
||
145 | |||
146 | return redirect($redirect_url); |
||
147 | } |
||
149 |