Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.73% |
86 / 88 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| AbstractSqlNotificationService | |
97.73% |
86 / 88 |
|
75.00% |
6 / 8 |
14 | |
0.00% |
0 / 1 |
| sendNotification | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getNotifications | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| getPersistence | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| getNotification | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
2.00 | |||
| getAllFieldString | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| notificationCount | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
3 | |||
| changeStatus | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| createNotification | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| prepareNotification | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\notification\implementation; |
| 24 | |
| 25 | use common_exception_NotFound; |
| 26 | use common_persistence_Persistence as Persistence; |
| 27 | use common_persistence_SqlPersistence; |
| 28 | use Doctrine\DBAL\Driver\Statement; |
| 29 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 30 | use oat\generis\model\OntologyAwareTrait; |
| 31 | use oat\generis\persistence\PersistenceManager; |
| 32 | use oat\generis\persistence\sql\SchemaProviderInterface; |
| 33 | use oat\tao\model\notification\AbstractNotificationService; |
| 34 | use oat\tao\model\notification\Notification; |
| 35 | |
| 36 | /** |
| 37 | * @deprecated This class is used by client only. It will be moved to client specific extension |
| 38 | */ |
| 39 | abstract class AbstractSqlNotificationService extends AbstractNotificationService implements SchemaProviderInterface |
| 40 | { |
| 41 | use OntologyAwareTrait; |
| 42 | |
| 43 | public const NOTIFICATION_TABLE = 'notifications'; |
| 44 | |
| 45 | public const NOTIFICATION_FIELD_ID = 'id'; |
| 46 | public const NOTIFICATION_FIELD_RECIPIENT = 'recipient'; |
| 47 | public const NOTIFICATION_FIELD_TITLE = 'title'; |
| 48 | public const NOTIFICATION_FIELD_STATUS = 'status'; |
| 49 | public const NOTIFICATION_FIELD_SENDER = 'sender_id'; |
| 50 | public const NOTIFICATION_FIELD_SENDER_NAME = 'sender_name'; |
| 51 | public const NOTIFICATION_FIELD_MESSAGE = 'message'; |
| 52 | public const NOTIFICATION_FIELD_CREATION = 'created_at'; |
| 53 | public const NOTIFICATION_FIELD_UPDATED = 'updated_at'; |
| 54 | |
| 55 | public const OPTION_PERSISTENCE = 'persistence'; |
| 56 | |
| 57 | public const DEFAULT_PERSISTENCE = 'default'; |
| 58 | |
| 59 | /** |
| 60 | * @var Persistence |
| 61 | */ |
| 62 | protected $persistence; |
| 63 | |
| 64 | public function sendNotification(Notification $notification): Notification |
| 65 | { |
| 66 | $this->getPersistence()->insert(self::NOTIFICATION_TABLE, $this->prepareNotification($notification)); |
| 67 | return $notification; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return Notification[] |
| 72 | */ |
| 73 | public function getNotifications(string $userId): array |
| 74 | { |
| 75 | $notification = []; |
| 76 | $persistence = $this->getPersistence(); |
| 77 | |
| 78 | $selectQuery = 'SELECT ' . self::NOTIFICATION_FIELD_ID . ' , ' . $this->getAllFieldString() . |
| 79 | ' FROM ' . self::NOTIFICATION_TABLE . ' WHERE ' . |
| 80 | self::NOTIFICATION_FIELD_RECIPIENT . ' = ? ' . |
| 81 | 'ORDER BY ' . self::NOTIFICATION_FIELD_CREATION . ' DESC ' . |
| 82 | 'LIMIT 20'; |
| 83 | |
| 84 | $params = [ |
| 85 | $userId |
| 86 | ]; |
| 87 | |
| 88 | $result = $persistence->query($selectQuery, $params)->fetchAll(); |
| 89 | |
| 90 | foreach ($result as $notificationDetail) { |
| 91 | $notification[] = $this->createNotification($notificationDetail); |
| 92 | } |
| 93 | |
| 94 | return $notification; |
| 95 | } |
| 96 | |
| 97 | protected function getPersistence(): Persistence |
| 98 | { |
| 99 | if ($this->persistence === null) { |
| 100 | $persistence = self::DEFAULT_PERSISTENCE; |
| 101 | |
| 102 | if ($this->hasOption(self::OPTION_PERSISTENCE)) { |
| 103 | $persistence = $this->getOption(self::OPTION_PERSISTENCE); |
| 104 | } |
| 105 | |
| 106 | $persistenceManager = $this->getServiceLocator()->get(PersistenceManager::SERVICE_ID); |
| 107 | $this->persistence = $persistenceManager->getPersistenceById($persistence); |
| 108 | } |
| 109 | return $this->persistence; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @throws common_exception_NotFound |
| 114 | */ |
| 115 | public function getNotification(string $id): Notification |
| 116 | { |
| 117 | /** @var common_persistence_SqlPersistence $persistence */ |
| 118 | $persistence = $this->getPersistence(); |
| 119 | |
| 120 | $selectQuery = 'SELECT ' . self::NOTIFICATION_FIELD_ID . ' , ' . $this->getAllFieldString() . |
| 121 | ' FROM ' . self::NOTIFICATION_TABLE . ' WHERE ' . |
| 122 | self::NOTIFICATION_FIELD_ID . ' = ? '; |
| 123 | |
| 124 | $params = [ |
| 125 | $id, |
| 126 | ]; |
| 127 | |
| 128 | $notificationDetail = $persistence->query($selectQuery, $params)->fetch(); |
| 129 | |
| 130 | if ($notificationDetail) { |
| 131 | return $this->createNotification($notificationDetail); |
| 132 | } |
| 133 | |
| 134 | throw new common_exception_NotFound('Error notification not found , requested id: ' . $id); |
| 135 | } |
| 136 | |
| 137 | protected function getAllFieldString(): string |
| 138 | { |
| 139 | return self::NOTIFICATION_FIELD_RECIPIENT . ' , ' |
| 140 | . self::NOTIFICATION_FIELD_STATUS . ' , ' |
| 141 | . self::NOTIFICATION_FIELD_SENDER . ' , ' |
| 142 | . self::NOTIFICATION_FIELD_SENDER_NAME . ' , ' |
| 143 | . self::NOTIFICATION_FIELD_TITLE . ' , ' |
| 144 | . self::NOTIFICATION_FIELD_MESSAGE . ' , ' |
| 145 | . self::NOTIFICATION_FIELD_CREATION . ' , ' |
| 146 | . self::NOTIFICATION_FIELD_UPDATED; |
| 147 | } |
| 148 | |
| 149 | public function notificationCount(string $userId): array |
| 150 | { |
| 151 | $persistence = $this->getPersistence(); |
| 152 | $count = [ Notification::CREATED_STATUS => 0 ]; |
| 153 | |
| 154 | $selectQuery = 'SELECT ' |
| 155 | . self::NOTIFICATION_FIELD_STATUS |
| 156 | . ' , COUNT(' |
| 157 | . self::NOTIFICATION_FIELD_ID |
| 158 | . ') as cpt' |
| 159 | . ' FROM ' |
| 160 | . self::NOTIFICATION_TABLE |
| 161 | . ' WHERE ' |
| 162 | . self::NOTIFICATION_FIELD_RECIPIENT |
| 163 | . ' = ? ' |
| 164 | . ' GROUP BY ' |
| 165 | . self::NOTIFICATION_FIELD_STATUS; |
| 166 | |
| 167 | $params = [ |
| 168 | $userId, |
| 169 | ]; |
| 170 | |
| 171 | /** @var Statement $statement */ |
| 172 | $statement = $persistence->query($selectQuery, $params); |
| 173 | |
| 174 | if (($result = $statement->fetchAll()) !== false) { |
| 175 | foreach ($result as $statusCount) { |
| 176 | $count[$statusCount[self::NOTIFICATION_FIELD_STATUS]] = $statusCount['cpt']; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return $count; |
| 181 | } |
| 182 | |
| 183 | public function changeStatus(Notification $notification): bool |
| 184 | { |
| 185 | $updateQuery = 'UPDATE ' . self::NOTIFICATION_TABLE . ' SET ' . |
| 186 | self::NOTIFICATION_FIELD_UPDATED . ' = ? ,' . |
| 187 | self::NOTIFICATION_FIELD_STATUS . ' = ? ' . |
| 188 | ' WHERE ' . self::NOTIFICATION_FIELD_ID . ' = ? '; |
| 189 | |
| 190 | $persistence = $this->getPersistence(); |
| 191 | /** @var AbstractPlatform $platform */ |
| 192 | $platform = $this->getPersistence()->getPlatForm(); |
| 193 | |
| 194 | $data = |
| 195 | [ |
| 196 | $platform->getNowExpression(), |
| 197 | $notification->getStatus(), |
| 198 | $notification->getId(), |
| 199 | ]; |
| 200 | |
| 201 | return (bool)$persistence->exec($updateQuery, $data); |
| 202 | } |
| 203 | |
| 204 | private function createNotification(array $notificationDetail): Notification |
| 205 | { |
| 206 | return new Notification( |
| 207 | $notificationDetail[self::NOTIFICATION_FIELD_RECIPIENT], |
| 208 | $notificationDetail[self::NOTIFICATION_FIELD_TITLE], |
| 209 | $notificationDetail[self::NOTIFICATION_FIELD_MESSAGE], |
| 210 | $notificationDetail[self::NOTIFICATION_FIELD_SENDER], |
| 211 | $notificationDetail[self::NOTIFICATION_FIELD_SENDER_NAME], |
| 212 | $notificationDetail[self::NOTIFICATION_FIELD_ID], |
| 213 | $notificationDetail[self::NOTIFICATION_FIELD_CREATION], |
| 214 | $notificationDetail[self::NOTIFICATION_FIELD_UPDATED], |
| 215 | $notificationDetail[self::NOTIFICATION_FIELD_STATUS] |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | abstract protected function prepareNotification(Notification $notification): array; |
| 220 | } |