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