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