Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
41 | class MailQueueHandler { |
||
42 | /** Number of entries we want to list in the email */ |
||
43 | const ENTRY_LIMIT = 200; |
||
44 | |||
45 | /** @var array */ |
||
46 | protected $languages; |
||
47 | |||
48 | /** @var string */ |
||
49 | protected $senderAddress; |
||
50 | |||
51 | /** @var string */ |
||
52 | protected $senderName; |
||
53 | |||
54 | /** @var IDateTimeFormatter */ |
||
55 | protected $dateFormatter; |
||
56 | |||
57 | /** @var DataHelper */ |
||
58 | protected $dataHelper; |
||
59 | |||
60 | /** @var IDBConnection */ |
||
61 | protected $connection; |
||
62 | |||
63 | /** @var IMailer */ |
||
64 | protected $mailer; |
||
65 | |||
66 | /** @var IURLGenerator */ |
||
67 | protected $urlGenerator; |
||
68 | |||
69 | /** @var IUserManager */ |
||
70 | protected $userManager; |
||
71 | |||
72 | /** @var IManager */ |
||
73 | protected $activityManager; |
||
74 | |||
75 | /** |
||
76 | * Constructor |
||
77 | * |
||
78 | * @param IDateTimeFormatter $dateFormatter |
||
79 | * @param IDBConnection $connection |
||
80 | * @param DataHelper $dataHelper |
||
81 | * @param IMailer $mailer |
||
82 | * @param IURLGenerator $urlGenerator |
||
83 | * @param IUserManager $userManager |
||
84 | * @param IManager $activityManager |
||
85 | */ |
||
86 | 10 | View Code Duplication | public function __construct(IDateTimeFormatter $dateFormatter, |
101 | |||
102 | /** |
||
103 | * Get the users we want to send an email to |
||
104 | * |
||
105 | * @param int|null $limit |
||
106 | * @param int $latestSend |
||
107 | * @return array |
||
108 | */ |
||
109 | 7 | public function getAffectedUsers($limit, $latestSend) { |
|
110 | 7 | $limit = (!$limit) ? null : (int) $limit; |
|
111 | |||
112 | 7 | $query = $this->connection->prepare( |
|
113 | 'SELECT `amq_affecteduser`, `email`, MIN(`amq_latest_send`) AS `amq_trigger_time` ' |
||
114 | . ' FROM `*PREFIX*activity_mq` ' |
||
115 | . ' LEFT JOIN `*PREFIX*accounts` ON `user_id` = `amq_affecteduser` ' |
||
116 | . ' WHERE `amq_latest_send` < ? ' |
||
117 | . ' GROUP BY `amq_affecteduser`, `email` ' |
||
118 | 7 | . ' ORDER BY `amq_trigger_time` ASC', |
|
119 | 7 | $limit); |
|
120 | 7 | $query->execute([$latestSend]); |
|
121 | |||
122 | 7 | $affectedUsers = []; |
|
123 | 7 | while ($row = $query->fetch()) { |
|
124 | 6 | $affectedUsers[] = [ |
|
125 | 6 | 'uid' => $row['amq_affecteduser'], |
|
126 | 6 | 'email' => $row['email'] |
|
127 | ]; |
||
128 | } |
||
129 | |||
130 | 7 | return $affectedUsers; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get all items for the user we want to send an email to |
||
135 | * |
||
136 | * @param string $affectedUser |
||
137 | * @param int $maxTime |
||
138 | * @param int $maxNumItems |
||
139 | * @return array [data of the first max. 200 entries, total number of entries] |
||
140 | */ |
||
141 | 7 | protected function getItemsForUser($affectedUser, $maxTime, $maxNumItems = self::ENTRY_LIMIT) { |
|
142 | 7 | $query = $this->connection->prepare( |
|
143 | 'SELECT * ' |
||
144 | . ' FROM `*PREFIX*activity_mq` ' |
||
145 | . ' WHERE `amq_timestamp` <= ? ' |
||
146 | . ' AND `amq_affecteduser` = ? ' |
||
147 | 7 | . ' ORDER BY `amq_timestamp` ASC', |
|
148 | 7 | $maxNumItems |
|
149 | ); |
||
150 | 7 | $query->execute([(int) $maxTime, $affectedUser]); |
|
151 | |||
152 | 7 | $activities = []; |
|
153 | 7 | while ($row = $query->fetch()) { |
|
154 | 7 | $activities[] = $row; |
|
155 | } |
||
156 | |||
157 | 7 | if (isset($activities[$maxNumItems - 1])) { |
|
158 | // Reached the limit, run a query to get the actual count. |
||
159 | 1 | $query = $this->connection->prepare( |
|
160 | 'SELECT COUNT(*) AS `actual_count`' |
||
161 | . ' FROM `*PREFIX*activity_mq` ' |
||
162 | . ' WHERE `amq_timestamp` <= ? ' |
||
163 | 1 | . ' AND `amq_affecteduser` = ?' |
|
164 | ); |
||
165 | 1 | $query->execute([(int) $maxTime, $affectedUser]); |
|
166 | |||
167 | 1 | $row = $query->fetch(); |
|
168 | 1 | return [$activities, $row['actual_count'] - $maxNumItems]; |
|
169 | } else { |
||
170 | 7 | return [$activities, 0]; |
|
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Get a language object for a specific language |
||
176 | * |
||
177 | * @param string $lang Language identifier |
||
178 | * @return \OCP\IL10N Language object of $lang |
||
179 | */ |
||
180 | 1 | protected function getLanguage($lang) { |
|
181 | 1 | if (!isset($this->languages[$lang])) { |
|
182 | 1 | $this->languages[$lang] = Util::getL10N('activity', $lang); |
|
183 | } |
||
184 | |||
185 | 1 | return $this->languages[$lang]; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get the sender data |
||
190 | * @param string $setting Either `email` or `name` |
||
191 | * @return string |
||
192 | */ |
||
193 | 1 | protected function getSenderData($setting) { |
|
194 | 1 | if (empty($this->senderAddress)) { |
|
195 | 1 | $this->senderAddress = Util::getDefaultEmailAddress('no-reply'); |
|
196 | } |
||
197 | 1 | if (empty($this->senderName)) { |
|
198 | 1 | $defaults = new Defaults(); |
|
199 | 1 | $this->senderName = $defaults->getName(); |
|
200 | } |
||
201 | |||
202 | 1 | if ($setting === 'email') { |
|
203 | 1 | return $this->senderAddress; |
|
204 | } |
||
205 | 1 | return $this->senderName; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Send a notification to one user |
||
210 | * |
||
211 | * @param string $userName Username of the recipient |
||
212 | * @param string $email Email address of the recipient |
||
213 | * @param string $lang Selected language of the recipient |
||
214 | * @param string $timezone Selected timezone of the recipient |
||
215 | * @param int $maxTime |
||
216 | */ |
||
217 | 1 | public function sendEmailToUser($userName, $email, $lang, $timezone, $maxTime) { |
|
218 | 1 | $user = $this->userManager->get($userName); |
|
219 | 1 | if (!$user instanceof IUser) { |
|
220 | 1 | return; |
|
221 | } |
||
222 | |||
223 | 1 | list($mailData, $skippedCount) = $this->getItemsForUser($userName, $maxTime); |
|
224 | |||
225 | 1 | $l = $this->getLanguage($lang); |
|
226 | 1 | $parser = new PlainTextParser($l); |
|
227 | 1 | $this->dataHelper->setUser($userName); |
|
228 | 1 | $this->dataHelper->setL10n($l); |
|
229 | 1 | $this->activityManager->setCurrentUserId($userName); |
|
230 | |||
231 | 1 | $activityList = []; |
|
232 | 1 | foreach ($mailData as $activity) { |
|
233 | 1 | $event = $this->activityManager->generateEvent(); |
|
234 | 1 | $event->setApp($activity['amq_appid']) |
|
235 | 1 | ->setType($activity['amq_type']) |
|
236 | 1 | ->setTimestamp($activity['amq_timestamp']) |
|
237 | 1 | ->setSubject($activity['amq_subject'], []); |
|
238 | |||
239 | 1 | $relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay( |
|
240 | 1 | $activity['amq_timestamp'], |
|
241 | 1 | 'long', 'medium', |
|
242 | 1 | new \DateTimeZone($timezone), $l |
|
243 | ); |
||
244 | |||
245 | 1 | $activityList[] = [ |
|
246 | 1 | $parser->parseMessage( |
|
247 | 1 | $this->dataHelper->translation( |
|
248 | 1 | $activity['amq_appid'], $activity['amq_subject'], $this->dataHelper->getParameters($event, 'subject', $activity['amq_subjectparams']) |
|
249 | ) |
||
250 | ), |
||
251 | 1 | $relativeDateTime, |
|
252 | ]; |
||
253 | } |
||
254 | |||
255 | 1 | $alttext = new Template('activity', 'email.notification', '', false); |
|
256 | 1 | $alttext->assign('username', $user->getDisplayName()); |
|
257 | 1 | $alttext->assign('activities', $activityList); |
|
258 | 1 | $alttext->assign('skippedCount', $skippedCount); |
|
259 | 1 | $alttext->assign('owncloud_installation', $this->urlGenerator->getAbsoluteURL('/')); |
|
260 | 1 | $alttext->assign('overwriteL10N', $l); |
|
261 | 1 | $emailText = $alttext->fetchPage(); |
|
262 | |||
263 | 1 | $htmltext = new Template('activity', 'html.notification', '', false); |
|
264 | 1 | $htmltext->assign('username', $user->getDisplayName()); |
|
265 | 1 | $htmltext->assign('activities', $activityList); |
|
266 | 1 | $htmltext->assign('skippedCount', $skippedCount); |
|
267 | 1 | $htmltext->assign('owncloud_installation', $this->urlGenerator->getAbsoluteURL('/')); |
|
268 | 1 | $htmltext->assign('overwriteL10N', $l); |
|
269 | 1 | $htmlText = $htmltext->fetchPage(); |
|
270 | |||
271 | 1 | $message = $this->mailer->createMessage(); |
|
272 | 1 | $message->setTo([$email => $user->getDisplayName()]); |
|
273 | 1 | $message->setSubject((string) $l->t('Activity notification')); |
|
274 | 1 | $message->setHtmlBody($htmlText); |
|
275 | 1 | $message->setPlainBody($emailText); |
|
276 | 1 | $message->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]); |
|
277 | 1 | $this->mailer->send($message); |
|
278 | |||
279 | 1 | $this->activityManager->setCurrentUserId(null); |
|
280 | 1 | } |
|
281 | |||
282 | /** |
||
283 | * Delete all entries we dealt with |
||
284 | * |
||
285 | * @param array $affectedUsers |
||
286 | * @param int $maxTime |
||
287 | */ |
||
288 | 6 | public function deleteSentItems(array $affectedUsers, $maxTime) { |
|
303 | } |
||
304 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.