| Conditions | 13 |
| Paths | 69 |
| Total Lines | 65 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 82 | function schedule(ITip\Message $iTipMessage) { |
||
| 83 | |||
| 84 | // Not sending any emails if the system considers the update |
||
| 85 | // insignificant. |
||
| 86 | if (!$iTipMessage->significantChange) { |
||
| 87 | if (!$iTipMessage->scheduleStatus) { |
||
| 88 | $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email'; |
||
| 89 | } |
||
| 90 | return; |
||
| 91 | } |
||
| 92 | |||
| 93 | $summary = $iTipMessage->message->VEVENT->SUMMARY; |
||
| 94 | |||
| 95 | if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') { |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') { |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | // don't send out mails for events that already took place |
||
| 104 | if ($this->isEventInThePast($iTipMessage->message)) { |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | $sender = substr($iTipMessage->sender, 7); |
||
| 109 | $recipient = substr($iTipMessage->recipient, 7); |
||
| 110 | |||
| 111 | $senderName = ($iTipMessage->senderName) ? $iTipMessage->senderName : null; |
||
| 112 | $recipientName = ($iTipMessage->recipientName) ? $iTipMessage->recipientName : null; |
||
| 113 | |||
| 114 | $subject = 'SabreDAV iTIP message'; |
||
| 115 | switch (strtoupper($iTipMessage->method)) { |
||
| 116 | case 'REPLY' : |
||
| 117 | $subject = 'Re: ' . $summary; |
||
| 118 | break; |
||
| 119 | case 'REQUEST' : |
||
| 120 | $subject = $summary; |
||
| 121 | break; |
||
| 122 | case 'CANCEL' : |
||
| 123 | $subject = 'Cancelled: ' . $summary; |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | |||
| 127 | $contentType = 'text/calendar; charset=UTF-8; method=' . $iTipMessage->method; |
||
| 128 | |||
| 129 | $message = $this->mailer->createMessage(); |
||
| 130 | |||
| 131 | $message->setReplyTo([$sender => $senderName]) |
||
| 132 | ->setTo([$recipient => $recipientName]) |
||
| 133 | ->setSubject($subject) |
||
| 134 | ->setBody($iTipMessage->message->serialize(), $contentType); |
||
| 135 | try { |
||
| 136 | $failed = $this->mailer->send($message); |
||
| 137 | if ($failed) { |
||
| 138 | $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]); |
||
| 139 | $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; |
||
| 140 | } |
||
| 141 | $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip'; |
||
| 142 | } catch(\Exception $ex) { |
||
| 143 | $this->logger->logException($ex, ['app' => 'dav']); |
||
| 144 | $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 194 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.