ProductSyncService.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Sync; |
| 4 | |
| 5 | /** |
| 6 | * This syncs an individual product to a post asynchronously. |
| 7 | */ |
| 8 | class ProductSyncService { |
| 9 | /** |
| 10 | * Application instance. |
| 11 | * |
| 12 | * @var \SureCart\Application |
| 13 | */ |
| 14 | protected $app = null; |
| 15 | |
| 16 | /** |
| 17 | * Whether to show a notice. |
| 18 | * |
| 19 | * @var boolean |
| 20 | */ |
| 21 | protected $with_notice = false; |
| 22 | |
| 23 | /** |
| 24 | * Constructor. |
| 25 | * |
| 26 | * @param \SureCart\Application $app The application. |
| 27 | */ |
| 28 | public function __construct( $app ) { |
| 29 | $this->app = $app; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Whether to show a notice. |
| 34 | * |
| 35 | * @param boolean $with_notice Whether to show a notice. |
| 36 | * |
| 37 | * @return self |
| 38 | */ |
| 39 | public function withNotice( $with_notice = true ) { |
| 40 | $this->with_notice = $with_notice; |
| 41 | return $this; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the task. |
| 46 | * |
| 47 | * @return \SureCart\Sync\Tasks\ProductSyncTask |
| 48 | */ |
| 49 | protected function task() { |
| 50 | return $this->app->resolve( 'surecart.tasks.product.sync' )->withNotice( $this->with_notice ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Post sync service |
| 55 | * |
| 56 | * @return ProductsQueueProcess |
| 57 | */ |
| 58 | public function post() { |
| 59 | return $this->app->resolve( 'surecart.process.product_post.sync' ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Queue the sync for a later time. |
| 64 | * |
| 65 | * @param \SureCart\Models\Model $model The model. |
| 66 | * |
| 67 | * @return \SureCart\Queue\Async |
| 68 | */ |
| 69 | public function queue( \SureCart\Models\Model $model ) { |
| 70 | return $this->task()->queue( $model->id ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Check if the sync is scheduled. |
| 75 | * |
| 76 | * @param \SureCart\Models\Model $model The model. |
| 77 | * |
| 78 | * @return bool |
| 79 | */ |
| 80 | public function isScheduled( \SureCart\Models\Model $model ) { |
| 81 | return $this->task()->isScheduled( $model->id ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Cancel the sync for a later time. |
| 86 | * |
| 87 | * @param \SureCart\Models\Model $model The model. |
| 88 | * |
| 89 | * @return \SureCart\Queue\Async |
| 90 | */ |
| 91 | public function cancel( \SureCart\Models\Model $model ) { |
| 92 | return $this->task()->cancel( $model->id ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Run the model sync immediately. |
| 97 | * |
| 98 | * @param \SureCart\Models\Product $product The Product. |
| 99 | * |
| 100 | * @return \WP_Post|\WP_Error |
| 101 | */ |
| 102 | public function sync( \SureCart\Models\Product $product ) { |
| 103 | return $this->post()->sync( $product ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Run the model sync immediately. |
| 108 | * |
| 109 | * @param string $id The product id. |
| 110 | * |
| 111 | * @return \WP_Post|\WP_Error|false|null |
| 112 | */ |
| 113 | public function delete( string $id ) { |
| 114 | return $this->post()->delete( $id ); |
| 115 | } |
| 116 | } |
| 117 |