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