|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Represents a notification queue item. |
| 4 | + * |
| 5 | + * @package Scoped_Notify |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Scoped_Notify; |
| 9 | + |
| 10 | +// Exit if accessed directly. |
| 11 | +defined( 'ABSPATH' ) || exit; |
| 12 | + |
| 13 | +/** |
| 14 | + * Data class representing a single notification item from the queue. |
| 15 | + */ |
| 16 | +class Notification_Item { |
| 17 | + |
| 18 | + /** |
| 19 | + * Blog ID where the notification originates. |
| 20 | + * |
| 21 | + * @var int |
| 22 | + */ |
| 23 | + public int $blog_id; |
| 24 | + |
| 25 | + /** |
| 26 | + * Object ID (e.g., post ID, comment ID). |
| 27 | + * |
| 28 | + * @var int |
| 29 | + */ |
| 30 | + public int $object_id; |
| 31 | + |
| 32 | + /** |
| 33 | + * Object type (e.g., 'post', 'comment'). |
| 34 | + * |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + public string $object_type; |
| 38 | + |
| 39 | + /** |
| 40 | + * Trigger ID from the triggers table. |
| 41 | + * |
| 42 | + * @var int |
| 43 | + */ |
| 44 | + public int $trigger_id; |
| 45 | + |
| 46 | + /** |
| 47 | + * Reason for the notification (e.g., 'new_post', 'new_comment'). |
| 48 | + * |
| 49 | + * @var string |
| 50 | + */ |
| 51 | + public string $reason; |
| 52 | + |
| 53 | + /** |
| 54 | + * Schedule type for the notification. |
| 55 | + * |
| 56 | + * @var string|null |
| 57 | + */ |
| 58 | + public ?string $schedule_type; |
| 59 | + |
| 60 | + /** |
| 61 | + * Constructor. |
| 62 | + * |
| 63 | + * @param int $blog_id Blog ID. |
| 64 | + * @param int $object_id Object ID. |
| 65 | + * @param string $object_type Object type. |
| 66 | + * @param int $trigger_id Trigger ID. |
| 67 | + * @param string $reason Notification reason. |
| 68 | + * @param string|null $schedule_type Schedule type. |
| 69 | + */ |
| 70 | + public function __construct( |
| 71 | + int $blog_id, |
| 72 | + int $object_id, |
| 73 | + string $object_type, |
| 74 | + int $trigger_id, |
| 75 | + string $reason, |
| 76 | + ?string $schedule_type = null |
| 77 | + ) { |
| 78 | + $this->blog_id = $blog_id; |
| 79 | + $this->object_id = $object_id; |
| 80 | + $this->object_type = $object_type; |
| 81 | + $this->trigger_id = $trigger_id; |
| 82 | + $this->reason = $reason; |
| 83 | + $this->schedule_type = $schedule_type; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Creates a Notification_Item from a database row (stdClass). |
| 88 | + * |
| 89 | + * @param \stdClass $row Database row object. |
| 90 | + * @return self |
| 91 | + */ |
| 92 | + public static function from_db_row( \stdClass $row ): self { |
| 93 | + return new self( |
| 94 | + (int) $row->blog_id, |
| 95 | + (int) $row->object_id, |
| 96 | + (string) $row->object_type, |
| 97 | + (int) $row->trigger_id, |
| 98 | + (string) $row->reason, |
| 99 | + $row->schedule_type ?? null |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Formats the item data for logging messages. |
| 105 | + * |
| 106 | + * @return string Formatted string representation. |
| 107 | + */ |
| 108 | + public function format(): string { |
| 109 | + return sprintf( |
| 110 | + 'blog_id %d, object_id %d, object_type %s, trigger_id %d', |
| 111 | + $this->blog_id, |
| 112 | + $this->object_id, |
| 113 | + $this->object_type, |
| 114 | + $this->trigger_id |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments