Revenue.php
162 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Revenue\Repositories; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 7 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 8 | |
| 9 | /** |
| 10 | * Class Revenue |
| 11 | * @package Give\Revenue\Repositories |
| 12 | * |
| 13 | * Use this class to get data from "give_revenue" table. |
| 14 | * |
| 15 | * @since 2.9.0 |
| 16 | */ |
| 17 | class Revenue |
| 18 | { |
| 19 | /** |
| 20 | * Insert revenue. |
| 21 | * |
| 22 | * @since 2.9.0 |
| 23 | * |
| 24 | * @param array $revenueData |
| 25 | * |
| 26 | * @return bool|int |
| 27 | */ |
| 28 | public function insert($revenueData) |
| 29 | { |
| 30 | global $wpdb; |
| 31 | |
| 32 | // Validate revenue data |
| 33 | $this->validateNewRevenueData($revenueData); |
| 34 | |
| 35 | /** |
| 36 | * Filter revenue data before inserting to revenue table. |
| 37 | * |
| 38 | * @since 2.9.0 |
| 39 | */ |
| 40 | $revenueData = apply_filters('give_revenue_insert_data', $revenueData); |
| 41 | |
| 42 | return DB::insert( |
| 43 | $wpdb->give_revenue, |
| 44 | $revenueData, |
| 45 | $this->getPlaceholderForPrepareQuery($revenueData) |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Deletes revenue |
| 51 | * |
| 52 | * @param $revenueId |
| 53 | * |
| 54 | * @return false|int |
| 55 | * @throws DatabaseQueryException |
| 56 | */ |
| 57 | public function deleteByDonationId($revenueId) |
| 58 | { |
| 59 | global $wpdb; |
| 60 | |
| 61 | try { |
| 62 | return DB::delete( |
| 63 | $wpdb->give_revenue, |
| 64 | ['donation_id' => $revenueId], |
| 65 | ['%d'] |
| 66 | ); |
| 67 | } catch (DatabaseQueryException $exception) { |
| 68 | give_record_log( |
| 69 | 'Revenue delete by donation failed', |
| 70 | $exception->getLogOutput(), |
| 71 | 0, |
| 72 | 'database' |
| 73 | ); |
| 74 | |
| 75 | throw $exception; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Validate new revenue data. |
| 81 | * |
| 82 | * @since 2.9.0 |
| 83 | * @since 2.9.4 Mention donation id in exception message. |
| 84 | * |
| 85 | * @param array $array |
| 86 | */ |
| 87 | protected function validateNewRevenueData($array) |
| 88 | { |
| 89 | $required = ['donation_id', 'form_id', 'amount']; |
| 90 | |
| 91 | if (empty($array['donation_id'])) { |
| 92 | unset($array['donation_id']); |
| 93 | } |
| 94 | |
| 95 | if (empty($array['form_id'])) { |
| 96 | unset($array['form_id']); |
| 97 | } |
| 98 | |
| 99 | if ( ! is_numeric($array['amount']) || (int)$array['amount'] < 0) { |
| 100 | unset($array['amount']); |
| 101 | } |
| 102 | |
| 103 | if (array_diff($required, array_keys($array))) { |
| 104 | $errorMessage = ''; |
| 105 | if (isset($array['donation_id'])) { |
| 106 | $errorMessage = "An error occurred when processing Donation #{$array['donation_id']}. "; |
| 107 | } |
| 108 | |
| 109 | throw new InvalidArgumentException( |
| 110 | sprintf( |
| 111 | '%2$sTo insert revenue, please provide valid %1$s.', |
| 112 | implode(', ', $required), |
| 113 | $errorMessage |
| 114 | ) |
| 115 | ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get placeholder for prepare query. |
| 121 | * |
| 122 | * @param array $data |
| 123 | * |
| 124 | * @return string[] Array of value format type |
| 125 | */ |
| 126 | private function getPlaceholderForPrepareQuery($data) |
| 127 | { |
| 128 | $format = []; |
| 129 | |
| 130 | foreach ($data as $value) { |
| 131 | $format[] = is_numeric($value) ? '%d' : '%s'; |
| 132 | } |
| 133 | |
| 134 | return $format; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Return whether or not donation id exist in give_revenue table. |
| 139 | * |
| 140 | * @sicne 2.9.0 |
| 141 | * |
| 142 | * @param int $donationId |
| 143 | * |
| 144 | * @return bool |
| 145 | */ |
| 146 | public function isDonationExist($donationId) |
| 147 | { |
| 148 | global $wpdb; |
| 149 | |
| 150 | return (bool)DB::get_var( |
| 151 | DB::prepare( |
| 152 | " |
| 153 | SELECT donation_id |
| 154 | FROM {$wpdb->give_revenue} |
| 155 | WHERE donation_id = %d |
| 156 | ", |
| 157 | $donationId |
| 158 | ) |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 |