# packeta/trunk/src/Packetery/Module/Product/ProductEntityFactory.php

Packeta, version trunk. 66 lines.

- Page: https://pluginprobe.com/plugins/packeta/trunk/code/src/Packetery/Module/Product/ProductEntityFactory.php
- Raw: https://pluginprobe.com/plugins/packeta/trunk/raw/src/Packetery/Module/Product/ProductEntityFactory.php
- Modified: 2025-03-10T10:47:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/packeta/trunk/code/src/Packetery/Module/Product/ProductEntityFactory.php#L10-L20`.

```php
<?php
/**
 * Class Product\ProductEntityFactory.
 *
 * @package Packetery\Module\Product
 */

declare( strict_types=1 );

namespace Packetery\Module\Product;

use Packetery\Module\Exception\ProductNotFoundException;
use Packetery\Module\Framework\WcAdapter;

/**
 * Class Product\ProductEntityFactory.
 *
 * @package Packetery\Module\Product
 */
class ProductEntityFactory {

	/**
	 * WP adapter.
	 *
	 * @var WcAdapter
	 */
	private $wcAdapter;

	/**
	 * Constructor.
	 *
	 * @param WcAdapter $wcAdapter WC adapter.
	 */
	public function __construct( WcAdapter $wcAdapter ) {
		$this->wcAdapter = $wcAdapter;
	}

	/**
	 * Create instance from post ID.
	 *
	 * @param int|string $postId Post ID.
	 *
	 * @return Entity
	 * @throws ProductNotFoundException Product not found.
	 */
	public function fromPostId( $postId ): Entity {
		$product = $this->wcAdapter->getProduct( $postId );
		if ( ! ( $product instanceof \WC_Product ) ) {
			throw new ProductNotFoundException( "Product $postId not found." );
		}

		return new Entity( $product );
	}

	/**
	 * Creates instance using global variables.
	 *
	 * @return Entity
	 */
	public function fromGlobals(): Entity {
		global $post;

		return $this->fromPostId( $post->ID );
	}
}

```
