| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
namespace Yoast\WP\SEO\Schema_Aggregator\Application\Schema_Map; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Schema_Aggregator\Application\Aggregate_Site_Schema_Map_Command; |
| 6 |
use Yoast\WP\SEO\Schema_Aggregator\Application\Aggregate_Site_Schema_Map_Command_Handler; |
| 7 |
use Yoast\WP\SEO\Schema_Aggregator\Application\Cache\Xml_Manager; |
| 8 |
use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Aggregator_Config; |
| 9 |
|
| 10 |
/** |
| 11 |
* Provides the schema map XML, building and caching it when needed. |
| 12 |
*/ |
| 13 |
class Schema_Map_Xml_Provider { |
| 14 |
|
| 15 |
/** |
| 16 |
* The command handler instance. |
| 17 |
* |
| 18 |
* @var Aggregate_Site_Schema_Map_Command_Handler |
| 19 |
*/ |
| 20 |
private $aggregate_site_schema_map_command_handler; |
| 21 |
|
| 22 |
/** |
| 23 |
* The XML cache manager instance. |
| 24 |
* |
| 25 |
* @var Xml_Manager |
| 26 |
*/ |
| 27 |
private $xml_cache_manager; |
| 28 |
|
| 29 |
/** |
| 30 |
* The aggregator configuration instance. |
| 31 |
* |
| 32 |
* @var Aggregator_Config |
| 33 |
*/ |
| 34 |
private $aggregator_config; |
| 35 |
|
| 36 |
/** |
| 37 |
* Schema_Map_Xml_Provider constructor. |
| 38 |
* |
| 39 |
* @param Aggregate_Site_Schema_Map_Command_Handler $aggregate_site_schema_map_command_handler The command handler. |
| 40 |
* @param Xml_Manager $xml_cache_manager The XML cache |
| 41 |
* manager. |
| 42 |
* @param Aggregator_Config $aggregator_config The aggregator |
| 43 |
* configuration. |
| 44 |
*/ |
| 45 |
public function __construct( |
| 46 |
Aggregate_Site_Schema_Map_Command_Handler $aggregate_site_schema_map_command_handler, |
| 47 |
Xml_Manager $xml_cache_manager, |
| 48 |
Aggregator_Config $aggregator_config |
| 49 |
) { |
| 50 |
$this->aggregate_site_schema_map_command_handler = $aggregate_site_schema_map_command_handler; |
| 51 |
$this->xml_cache_manager = $xml_cache_manager; |
| 52 |
$this->aggregator_config = $aggregator_config; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the schema map XML, building and caching it when it is not cached yet. |
| 57 |
* |
| 58 |
* @return string The schema map XML. |
| 59 |
*/ |
| 60 |
public function get_xml(): string { |
| 61 |
$cached_xml = $this->xml_cache_manager->get(); |
| 62 |
if ( $cached_xml !== null ) { |
| 63 |
return $cached_xml; |
| 64 |
} |
| 65 |
|
| 66 |
$command = new Aggregate_Site_Schema_Map_Command( $this->aggregator_config->get_allowed_post_types() ); |
| 67 |
$xml = $this->aggregate_site_schema_map_command_handler->handle( $command ); |
| 68 |
|
| 69 |
$this->xml_cache_manager->set( $xml ); |
| 70 |
|
| 71 |
return $xml; |
| 72 |
} |
| 73 |
} |
| 74 |
|