ReturnRequest.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Models\Model; |
| 6 | use SureCart\Models\Traits\HasDates; |
| 7 | use SureCart\Models\Traits\HasOrder; |
| 8 | |
| 9 | /** |
| 10 | * ReturnRequest model. |
| 11 | */ |
| 12 | class ReturnRequest extends Model { |
| 13 | use HasOrder, HasDates; |
| 14 | |
| 15 | /** |
| 16 | * Rest API endpoint |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $endpoint = 'return_requests'; |
| 21 | |
| 22 | /** |
| 23 | * Object name |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $object_name = 'return_request'; |
| 28 | |
| 29 | /** |
| 30 | * Open a return request. |
| 31 | * |
| 32 | * @param string $id Model id. |
| 33 | * @return $this|\WP_Error |
| 34 | */ |
| 35 | protected function open( $id = null ) { |
| 36 | if ( $id ) { |
| 37 | $this->setAttribute( 'id', $id ); |
| 38 | } |
| 39 | |
| 40 | if ( $this->fireModelEvent( 'opening' ) === false ) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | if ( empty( $this->id ) ) { |
| 45 | return new \WP_Error( 'not_saved', 'You can only open an existing return request.' ); |
| 46 | } |
| 47 | |
| 48 | $attributes = $this->attributes; |
| 49 | unset( $attributes['id'] ); |
| 50 | |
| 51 | $opened = $this->makeRequest( |
| 52 | [ |
| 53 | 'method' => 'PATCH', |
| 54 | 'query' => $this->query, |
| 55 | 'body' => [ |
| 56 | $this->object_name => $attributes, |
| 57 | ], |
| 58 | ], |
| 59 | $this->endpoint . '/' . $this->id . '/open/' |
| 60 | ); |
| 61 | |
| 62 | if ( is_wp_error( $opened ) ) { |
| 63 | return $opened; |
| 64 | } |
| 65 | |
| 66 | $this->resetAttributes(); |
| 67 | $this->fill( $opened ); |
| 68 | $this->fireModelEvent( 'opened' ); |
| 69 | |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Complete a return request. |
| 75 | * |
| 76 | * @param string $id Model id. |
| 77 | * @return $this|\WP_Error |
| 78 | */ |
| 79 | protected function complete( $id = null ) { |
| 80 | if ( $id ) { |
| 81 | $this->setAttribute( 'id', $id ); |
| 82 | } |
| 83 | |
| 84 | if ( $this->fireModelEvent( 'completing' ) === false ) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if ( empty( $this->id ) ) { |
| 89 | return new \WP_Error( 'not_saved', 'You can only complete an existing return request.' ); |
| 90 | } |
| 91 | |
| 92 | $attributes = $this->attributes; |
| 93 | unset( $attributes['id'] ); |
| 94 | |
| 95 | $completed = $this->makeRequest( |
| 96 | [ |
| 97 | 'method' => 'PATCH', |
| 98 | 'query' => $this->query, |
| 99 | 'body' => [ |
| 100 | $this->object_name => $attributes, |
| 101 | ], |
| 102 | ], |
| 103 | $this->endpoint . '/' . $this->id . '/complete/' |
| 104 | ); |
| 105 | |
| 106 | if ( is_wp_error( $completed ) ) { |
| 107 | return $completed; |
| 108 | } |
| 109 | |
| 110 | $this->resetAttributes(); |
| 111 | $this->fill( $completed ); |
| 112 | $this->fireModelEvent( 'completed' ); |
| 113 | |
| 114 | return $this; |
| 115 | } |
| 116 | } |
| 117 |