Factory.php
| 1 | <?php |
| 2 | /** |
| 3 | * Notification Factory |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types = 1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\StockNotifications; |
| 9 | |
| 10 | use Automattic\WooCommerce\Internal\StockNotifications\Notification; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Notification factory class |
| 16 | */ |
| 17 | class Factory { |
| 18 | |
| 19 | /** |
| 20 | * Get the notification object. |
| 21 | * |
| 22 | * @param int $notification_id Notification ID to get. |
| 23 | * @return Notification|bool |
| 24 | */ |
| 25 | public static function get_notification( int $notification_id ) { |
| 26 | |
| 27 | if ( ! $notification_id ) { |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | try { |
| 32 | $notification = new Notification( $notification_id ); |
| 33 | return $notification; |
| 34 | } catch ( \Exception $e ) { |
| 35 | \wc_caught_exception( $e, __FUNCTION__, array( $notification_id ) ); |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Create a dummy notification for preview/testing purposes. |
| 42 | * |
| 43 | * @return Notification |
| 44 | */ |
| 45 | public static function create_dummy_notification(): Notification { |
| 46 | $notification = new Notification(); |
| 47 | |
| 48 | // Create a dummy product. |
| 49 | $product = new \WC_Product(); |
| 50 | $product->set_name( __( 'Dummy Product', 'woocommerce' ) ); |
| 51 | $product->set_price( 25 ); |
| 52 | $product->set_image_id( get_option( 'woocommerce_placeholder_image', 0 ) ); |
| 53 | |
| 54 | // Set required notification data. |
| 55 | $notification->set_product_id( $product->get_id() ); |
| 56 | $notification->set_user_email( 'preview@example.com' ); |
| 57 | |
| 58 | // Store the dummy product in the notification object for preview. |
| 59 | $notification->product = $product; |
| 60 | |
| 61 | return $notification; |
| 62 | } |
| 63 | } |
| 64 |