i-lateral /
silverstripe-notifications
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ilateral\SilverStripe\Notifier; |
||
| 4 | |||
| 5 | use SilverStripe\ORM\SS_List; |
||
| 6 | use SilverStripe\ORM\DataObject; |
||
| 7 | use SilverStripe\Core\Config\Configurable; |
||
| 8 | use SilverStripe\Core\Injector\Injectable; |
||
| 9 | use ilateral\SilverStripe\Notifier\Model\Notification; |
||
| 10 | use ilateral\SilverStripe\Notifier\Types\NotificationType; |
||
| 11 | use SilverStripe\Core\Injector\Injector; |
||
| 12 | |||
| 13 | class Notifier |
||
| 14 | { |
||
| 15 | use Injectable, Configurable; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Register data objects for notification and also |
||
| 19 | * stipulate which notifications they can recieve. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private static $registered_objects = []; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * Generate a list of reguistered objects with the object's |
||
| 27 | * short name as value |
||
| 28 | */ |
||
| 29 | public static function getRegisteredObjectsArray(): array |
||
| 30 | { |
||
| 31 | $list = self::config()->get('registered_objects'); |
||
| 32 | $return = []; |
||
| 33 | |||
| 34 | foreach ($list as $classname) { |
||
| 35 | /** @var DataObject */ |
||
| 36 | $obj = Injector::inst()->get($classname); |
||
| 37 | $return[$classname] = $obj->i18n_singular_name(); |
||
| 38 | } |
||
| 39 | |||
| 40 | return $return; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Process and send a list of notifications based on their provided type |
||
| 45 | */ |
||
| 46 | public static function processNotifications(SS_List $notifications, DataObject $object) |
||
| 47 | { |
||
| 48 | /** @var Notification $notification */ |
||
| 49 | foreach ($notifications as $notification) { |
||
| 50 | foreach ($notification->Types() as $notification_type) { |
||
| 51 | /** @var NotificationType $notification_type */ |
||
| 52 | $notification_type->setObject($object); |
||
| 53 | $notification_type->send(); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | } |
||
| 57 | } |
||
| 58 |