]> git.mxchange.org Git - friendica.git/blob - src/Model/ItemDeliveryData.php
1cd9c4f448acb897a8817bf507dfe05f12a6fdc1
[friendica.git] / src / Model / ItemDeliveryData.php
1 <?php
2
3 /**
4  * @file src/Model/ItemDeliveryData.php
5  */
6
7 namespace Friendica\Model;
8
9 use Friendica\Database\DBA;
10
11 class ItemDeliveryData
12 {
13         const LEGACY_FIELD_LIST = [
14                 // Legacy fields moved from item table
15                 'postopts',
16                 'inform',
17         ];
18
19         const FIELD_LIST = [
20                 // New delivery fields with virtual field name in item fields
21                 'queue_count' => 'delivery_queue_count',
22                 'queue_done'  => 'delivery_queue_done',
23         ];
24
25         /**
26          * Extract delivery data from the provided item fields
27          *
28          * @param array $fields
29          * @return array
30          */
31         public static function extractFields(array &$fields)
32         {
33                 $delivery_data = [];
34                 foreach (array_merge(ItemDeliveryData::FIELD_LIST, ItemDeliveryData::LEGACY_FIELD_LIST) as $key => $field) {
35                         if (is_int($key) && isset($fields[$field])) {
36                                 // Legacy field moved from item table
37                                 $delivery_data[$field] = $fields[$field];
38                                 $fields[$field] = null;
39                         } elseif (isset($fields[$field])) {
40                                 // New delivery field with virtual field name in item fields
41                                 $delivery_data[$key] = $fields[$field];
42                                 unset($fields[$field]);
43                         }
44                 }
45
46                 return $delivery_data;
47         }
48
49         /**
50          * Increments the queue_done for the given item ID.
51          *
52          * Avoids racing condition between multiple delivery threads.
53          *
54          * @param integer $item_id
55          * @return bool
56          * @throws \Exception
57          */
58         public static function incrementQueueDone($item_id)
59         {
60                 return DBA::e('UPDATE `item-delivery-data` SET `queue_done` = `queue_done` + 1 WHERE `iid` = ?', $item_id);
61         }
62
63         /**
64          * Insert a new item delivery data entry
65          *
66          * @param integer $item_id
67          * @param array   $fields
68          * @return bool
69          * @throws \Exception
70          */
71         public static function insert($item_id, array $fields)
72         {
73                 if (empty($item_id)) {
74                         throw new \BadMethodCallException('Empty item_id');
75                 }
76
77                 $fields['iid'] = $item_id;
78
79                 return DBA::insert('item-delivery-data', $fields);
80         }
81
82         /**
83          * Update/Insert item delivery data
84          *
85          * If you want to update queue_done, please use incrementQueueDone instead.
86          *
87          * @param integer $item_id
88          * @param array   $fields
89          * @return bool
90          * @throws \Exception
91          */
92         public static function update($item_id, array $fields)
93         {
94                 if (empty($item_id)) {
95                         throw new \BadMethodCallException('Empty item_id');
96                 }
97
98                 if (empty($fields)) {
99                         // Nothing to do, update successful
100                         return true;
101                 }
102
103                 return DBA::update('item-delivery-data', $fields, ['iid' => $item_id], true);
104         }
105
106         /**
107          * Delete item delivery data
108          *
109          * @param integer $item_id
110          * @return bool
111          * @throws \Exception
112          */
113         public static function delete($item_id)
114         {
115                 if (empty($item_id)) {
116                         throw new \BadMethodCallException('Empty item_id');
117                 }
118
119                 return DBA::delete('item-delivery-data', ['iid' => $item_id]);
120         }
121 }