OrderFactory.php
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\Models\Order; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\FactoryInterface; |
| 6 | use ProfilePress\Core\Membership\Repositories\OrderRepository; |
| 7 | |
| 8 | class OrderFactory implements FactoryInterface |
| 9 | { |
| 10 | /** |
| 11 | * @param $data |
| 12 | * |
| 13 | * @return OrderEntity |
| 14 | */ |
| 15 | public static function make($data) |
| 16 | { |
| 17 | return new OrderEntity($data); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @param $id |
| 22 | * |
| 23 | * @return OrderEntity |
| 24 | */ |
| 25 | public static function fromId($id) |
| 26 | { |
| 27 | return ppress_cache_transform('order_factory_from_id_' . $id, function () use ($id) { |
| 28 | return OrderRepository::init()->retrieve(absint($id)); |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param $id |
| 34 | * |
| 35 | * @return OrderEntity|false |
| 36 | */ |
| 37 | public static function fromTransactionId($id) |
| 38 | { |
| 39 | $orders = OrderRepository::init()->retrieveBy([ |
| 40 | 'transaction_id' => $id |
| 41 | ]); |
| 42 | |
| 43 | if ( ! empty($orders)) return $orders[0]; |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param $order_key |
| 50 | * |
| 51 | * @return OrderEntity |
| 52 | */ |
| 53 | public static function fromOrderKey($order_key) |
| 54 | { |
| 55 | $order_key = sanitize_text_field($order_key); |
| 56 | |
| 57 | return ppress_cache_transform('order_factory_from_order_key_' . $order_key, function () use ($order_key) { |
| 58 | return OrderRepository::init()->retrieveByOrderKey($order_key); |
| 59 | }); |
| 60 | } |
| 61 | } |