| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Llms_Txt\Infrastructure\Content; |
| 5 |
|
| 6 |
use Exception; |
| 7 |
use Yoast\WP\SEO\Llms_Txt\Domain\content\Post_Collection_Interface; |
| 8 |
|
| 9 |
/** |
| 10 |
* The factory to determine which post collection class to use. |
| 11 |
*/ |
| 12 |
class Post_Collection_Factory { |
| 13 |
|
| 14 |
/** |
| 15 |
* The manual post collection. |
| 16 |
* |
| 17 |
* @var Manual_Post_Collection |
| 18 |
*/ |
| 19 |
private $manual_post_collection; |
| 20 |
|
| 21 |
/** |
| 22 |
* The automatic post collection. |
| 23 |
* |
| 24 |
* @var Automatic_Post_Collection |
| 25 |
*/ |
| 26 |
private $automatic_post_collection; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor. |
| 30 |
* |
| 31 |
* @param Manual_Post_Collection $manual_post_collection The manual post collection. |
| 32 |
* @param Automatic_Post_Collection $automatic_post_collection The automatic post collection. |
| 33 |
*/ |
| 34 |
public function __construct( Manual_Post_Collection $manual_post_collection, Automatic_Post_Collection $automatic_post_collection ) { |
| 35 |
$this->manual_post_collection = $manual_post_collection; |
| 36 |
$this->automatic_post_collection = $automatic_post_collection; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Determines which collection class is needed. |
| 41 |
* |
| 42 |
* @param string $collection_type The type of collection. |
| 43 |
* |
| 44 |
* @throws Exception Throws when an invalid type is given. |
| 45 |
* @return Post_Collection_Interface |
| 46 |
*/ |
| 47 |
public function get_post_collection( string $collection_type ): Post_Collection_Interface { |
| 48 |
switch ( $collection_type ) { |
| 49 |
case 'manual': |
| 50 |
return $this->manual_post_collection; |
| 51 |
case 'auto': |
| 52 |
return $this->automatic_post_collection; |
| 53 |
default: |
| 54 |
throw new Exception( 'Invalid collection type provided' ); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|