| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Models\ProductMeta; |
| 7 |
use FluentCart\Framework\Database\Orm\Builder; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class ProductMetaResource extends BaseResourceApi |
| 11 |
{ |
| 12 |
private static $metaKey = 'product_thumbnail'; |
| 13 |
|
| 14 |
private static $objectType = 'product_variant_info'; |
| 15 |
|
| 16 |
public static function getQuery(): Builder |
| 17 |
{ |
| 18 |
return ProductMeta::query(); |
| 19 |
} |
| 20 |
|
| 21 |
public static function get(array $params = []) |
| 22 |
{ |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Find metadata by specified product ID and given parameters. |
| 28 |
* |
| 29 |
* @param int $productId Required. The ID of the product to find metadata. |
| 30 |
* @param array $params Optional. Additional parameters for the metadata retrieval. |
| 31 |
* [ |
| 32 |
* // Include optional parameters, if any. |
| 33 |
* ] |
| 34 |
* |
| 35 |
*/ |
| 36 |
public static function find($productId, $params = []) |
| 37 |
{ |
| 38 |
return static::getQuery()->where('object_id', $productId)->where('object_type', static::$objectType)->where('meta_key', static::$metaKey)->first(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Create a new product meta with the given data |
| 43 |
* |
| 44 |
* @param array $data Required. Array containing the necessary parameters for file creation. |
| 45 |
* [ |
| 46 |
* 'object_id' => (int) Required. The id of the product, |
| 47 |
* 'object_type' => (string) Required. The type of the product meta, |
| 48 |
* 'meta_value' => (string) Required. The value of the product meta |
| 49 |
* [ |
| 50 |
* 'id' => (int) Required. The id of the thumbnail, |
| 51 |
* 'url' => (string) Required. The url of the thumbnail, |
| 52 |
* 'title' => (string) Required. The title of the thumbnail, |
| 53 |
* ] |
| 54 |
* 'meta_key' => (string) Required. The key of the product meta, |
| 55 |
* ] |
| 56 |
* @param array $params Optional. Additional parameters for meta creation. |
| 57 |
* [ |
| 58 |
* 'product_id' => (int) Required. The id of the product, |
| 59 |
* ] |
| 60 |
* |
| 61 |
*/ |
| 62 |
public static function create($data, $params = []) |
| 63 |
{ |
| 64 |
$productId = Arr::get($params, 'product_id'); |
| 65 |
|
| 66 |
$isCreated = static::getQuery()->create([ |
| 67 |
'object_id' => $productId, |
| 68 |
'object_type' => 'product_variant_info', |
| 69 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 70 |
'meta_value' => $data, |
| 71 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 72 |
'meta_key' => 'product_thumbnail', |
| 73 |
]); |
| 74 |
|
| 75 |
if($isCreated) { |
| 76 |
return static::makeSuccessResponse( |
| 77 |
$isCreated, |
| 78 |
__('Thumbnail set successfully', 'fluent-cart') |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
return static::makeErrorResponse([ |
| 83 |
[ 'code' => 400, 'message' => __('Failed to set thumbnail!', 'fluent-cart') ] |
| 84 |
]); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Update product meta with the given data |
| 89 |
* |
| 90 |
* @param array $data Required. Array containing the necessary parameters for file creation. |
| 91 |
* [ |
| 92 |
* 'meta_value' => (string) Required. The value of the product meta |
| 93 |
* [ |
| 94 |
* 'id' => (int) Required. The id of the thumbnail, |
| 95 |
* 'url' => (string) Required. The url of the thumbnail, |
| 96 |
* 'title' => (string) Required. The title of the thumbnail, |
| 97 |
* ] |
| 98 |
* ] |
| 99 |
* @param int $productId Required. The ID of the product meta. |
| 100 |
* @param array $params Optional. Additional parameters for meta creation. |
| 101 |
* [ |
| 102 |
* // Include optional parameters, if any. |
| 103 |
* ] |
| 104 |
* |
| 105 |
*/ |
| 106 |
public static function update($data, $productId, $params = []) |
| 107 |
{ |
| 108 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 109 |
$isUpdated = ProductMetaResource::find($productId)->update(['meta_value' => $data]); |
| 110 |
|
| 111 |
if($isUpdated) { |
| 112 |
return static::makeSuccessResponse( |
| 113 |
$isUpdated, |
| 114 |
__('Thumbnail set successfully!', 'fluent-cart') |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
return static::makeErrorResponse([ |
| 119 |
[ 'code' => 400, 'message' => __('Failed to set thumbnail!', 'fluent-cart') ] |
| 120 |
]); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Delete product meta with the given ID and parameters. |
| 125 |
* |
| 126 |
* @param int $id Required. The ID of the product meta. |
| 127 |
* @param array $params Optional. Additional parameters. |
| 128 |
* [ |
| 129 |
* // Include optional parameters, if any. |
| 130 |
* ] |
| 131 |
* |
| 132 |
*/ |
| 133 |
public static function delete($id, $params = []) |
| 134 |
{ |
| 135 |
$productMeta = ProductMetaResource::find($id); |
| 136 |
|
| 137 |
if (!empty($productMeta)) { |
| 138 |
$productMeta->delete(); |
| 139 |
return static::makeSuccessResponse( |
| 140 |
'', |
| 141 |
__('Thumbnail has been deleted successfully!', 'fluent-cart') |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
return static::makeErrorResponse([ |
| 146 |
[ 'code' => 400, 'message' => __('Failed to delete!', 'fluent-cart') ] |
| 147 |
]); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Find metadata by specified ids and given parameters. |
| 152 |
* |
| 153 |
* @param array $ids Required. The id of the product variants to find metadata. |
| 154 |
* @param array $params Optional. Additional parameters for the metadata retrieval. |
| 155 |
* [ |
| 156 |
* // Include optional parameters, if any. |
| 157 |
* ] |
| 158 |
*/ |
| 159 |
public static function findByIds($ids, $params = []) |
| 160 |
{ |
| 161 |
return static::getQuery() |
| 162 |
->select('object_id', 'meta_value') |
| 163 |
->where('object_type', static::$objectType) |
| 164 |
->where('meta_key', static::$metaKey) |
| 165 |
->whereIn('object_id', $ids) |
| 166 |
->get(); |
| 167 |
} |
| 168 |
} |