Total Complexity | 72 |
Total Lines | 550 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Notification 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.
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 Notification, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Notification extends Model |
||
10 | { |
||
11 | // mail_notify_message ("At once", "Daily", "No") |
||
12 | const NOTIFY_MESSAGE_AT_ONCE = 1; |
||
13 | const NOTIFY_MESSAGE_DAILY = 8; |
||
14 | const NOTIFY_MESSAGE_WEEKLY = 12; |
||
15 | const NOTIFY_MESSAGE_NO = 0; |
||
16 | |||
17 | // mail_notify_invitation ("At once", "Daily", "No") |
||
18 | const NOTIFY_INVITATION_AT_ONCE = 1; |
||
19 | const NOTIFY_INVITATION_DAILY = 8; |
||
20 | const NOTIFY_INVITATION_WEEKLY = 12; |
||
21 | const NOTIFY_INVITATION_NO = 0; |
||
22 | |||
23 | // mail_notify_group_message ("At once", "Daily", "No") |
||
24 | const NOTIFY_GROUP_AT_ONCE = 1; |
||
25 | const NOTIFY_GROUP_DAILY = 8; |
||
26 | const NOTIFY_GROUP_WEEKLY = 12; |
||
27 | const NOTIFY_GROUP_NO = 0; |
||
28 | |||
29 | // Notification types |
||
30 | const NOTIFICATION_TYPE_MESSAGE = 1; |
||
31 | const NOTIFICATION_TYPE_INVITATION = 2; |
||
32 | const NOTIFICATION_TYPE_GROUP = 3; |
||
33 | const NOTIFICATION_TYPE_WALL_MESSAGE = 4; |
||
34 | const NOTIFICATION_TYPE_DIRECT_MESSAGE = 5; |
||
35 | public $table; |
||
36 | public $columns = [ |
||
37 | 'id', |
||
38 | 'dest_user_id', |
||
39 | 'dest_mail', |
||
40 | 'title', |
||
41 | 'content', |
||
42 | 'send_freq', |
||
43 | 'created_at', |
||
44 | 'sent_at', |
||
45 | ]; |
||
46 | |||
47 | //Max length of the notification.content field |
||
48 | public $max_content_length = 254; |
||
49 | public $debug = false; |
||
50 | |||
51 | /* message, invitation, group messages */ |
||
52 | public $type; |
||
53 | public $adminName; |
||
54 | public $adminEmail; |
||
55 | public $titlePrefix; |
||
56 | |||
57 | /** |
||
58 | * Constructor. |
||
59 | */ |
||
60 | public function __construct() |
||
61 | { |
||
62 | // Get config from mail.conf.php, as loaded by global.inc.php |
||
63 | $platform_email = $GLOBALS['platform_email']; |
||
64 | $this->table = Database::get_main_table(TABLE_NOTIFICATION); |
||
65 | if (!empty($platform_email['SMTP_FROM_EMAIL'])) { |
||
66 | $this->adminEmail = $platform_email['SMTP_FROM_EMAIL']; |
||
67 | if (!empty($platform_email['SMTP_FROM_NAME'])) { |
||
68 | $this->adminName = $platform_email['SMTP_FROM_NAME']; |
||
69 | } |
||
70 | } else { |
||
71 | // Default no-reply email |
||
72 | $this->adminEmail = api_get_setting('noreply_email_address'); |
||
73 | $this->adminName = api_get_setting('siteName'); |
||
74 | $this->titlePrefix = '['.api_get_setting('siteName').'] '; |
||
75 | |||
76 | // If no-reply email doesn't exist use the admin name/email |
||
77 | if (empty($this->adminEmail)) { |
||
78 | $this->adminEmail = api_get_setting('emailAdministrator'); |
||
79 | $this->adminName = api_get_person_name( |
||
80 | api_get_setting('administratorName'), |
||
81 | api_get_setting('administratorSurname'), |
||
82 | null, |
||
83 | PERSON_NAME_EMAIL_ADDRESS |
||
84 | ); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getTitlePrefix() |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getDefaultPlatformSenderEmail() |
||
101 | { |
||
102 | return $this->adminEmail; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getDefaultPlatformSenderName() |
||
109 | { |
||
110 | return $this->adminName; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Send the notifications. |
||
115 | * |
||
116 | * @param int $frequency notification frequency |
||
117 | */ |
||
118 | public function send($frequency = 8) |
||
119 | { |
||
120 | $notifications = $this->find( |
||
121 | 'all', |
||
122 | ['where' => ['sent_at IS NULL AND send_freq = ?' => $frequency]] |
||
123 | ); |
||
124 | |||
125 | if (!empty($notifications)) { |
||
126 | foreach ($notifications as $item_to_send) { |
||
127 | // Sending email |
||
128 | api_mail_html( |
||
129 | $item_to_send['dest_mail'], |
||
130 | $item_to_send['dest_mail'], |
||
131 | Security::filter_terms($item_to_send['title']), |
||
132 | Security::filter_terms($item_to_send['content']), |
||
133 | $this->adminName, |
||
134 | $this->adminEmail |
||
135 | ); |
||
136 | if ($this->debug) { |
||
137 | error_log('Sending message to: '.$item_to_send['dest_mail']); |
||
138 | } |
||
139 | |||
140 | // Updating |
||
141 | $item_to_send['sent_at'] = api_get_utc_datetime(); |
||
142 | $this->update($item_to_send); |
||
143 | if ($this->debug) { |
||
144 | error_log('Updating record : '.print_r($item_to_send, 1)); |
||
|
|||
145 | } |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param string $title |
||
152 | * @param array $senderInfo |
||
153 | * @param bool $forceTitleWhenSendingEmail force the use of $title as subject instead of "You have a new message" |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function formatTitle($title, $senderInfo, $forceTitleWhenSendingEmail = false) |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Save message notification. |
||
229 | * |
||
230 | * @param int $type message type |
||
231 | * NOTIFICATION_TYPE_MESSAGE, |
||
232 | * NOTIFICATION_TYPE_INVITATION, |
||
233 | * NOTIFICATION_TYPE_GROUP |
||
234 | * @param int $messageId |
||
235 | * @param array $userList recipients: user list of ids |
||
236 | * @param string $title |
||
237 | * @param string $content |
||
238 | * @param array $senderInfo result of api_get_user_info() or GroupPortalManager:get_group_data() |
||
239 | * @param array $attachments |
||
240 | * @param array $smsParameters |
||
241 | * @param bool $forceTitleWhenSendingEmail force the use of $title as subject instead of "You have a new message" |
||
242 | */ |
||
243 | public function saveNotification( |
||
362 | } |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Formats the content in order to add the welcome message, |
||
367 | * the notification preference, etc. |
||
368 | * |
||
369 | * @param int $messageId |
||
370 | * @param string $content |
||
371 | * @param array $senderInfo result of api_get_user_info() or |
||
372 | * GroupPortalManager:get_group_data() |
||
373 | * |
||
374 | * @return string |
||
375 | * */ |
||
376 | public function formatContent($messageId, $content, $senderInfo) |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * Send the push notifications to Chamilo Mobile app. |
||
480 | * |
||
481 | * @param array $userIds The IDs of users who will be notified |
||
482 | * @param string $title The notification title |
||
483 | * @param string $content The notification content |
||
484 | * |
||
485 | * @return int The number of success notifications. Otherwise returns false |
||
486 | */ |
||
487 | public static function sendPushNotification(array $userIds, $title, $content) |
||
559 | } |
||
560 | } |
||
561 |