| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Promotions\Application; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Promotions\Domain\Promotion_Interface; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class to manage promotional promotions. |
| 9 |
* |
| 10 |
* @makePublic |
| 11 |
*/ |
| 12 |
class Promotion_Manager implements Promotion_Manager_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The centralized list of promotions: all promotions should be passed to the constructor. |
| 16 |
* |
| 17 |
* @var array<Abstract_Promotion> |
| 18 |
*/ |
| 19 |
private $promotions_list = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class constructor. |
| 23 |
* |
| 24 |
* @param Promotion_Interface ...$promotions List of promotions. |
| 25 |
*/ |
| 26 |
public function __construct( Promotion_Interface ...$promotions ) { |
| 27 |
$this->promotions_list = $promotions; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Whether the promotion is effective. |
| 32 |
* |
| 33 |
* @param string $promotion_name The name of the promotion. |
| 34 |
* |
| 35 |
* @return bool Whether the promotion is effective. |
| 36 |
*/ |
| 37 |
public function is( string $promotion_name ): bool { |
| 38 |
$time = \time(); |
| 39 |
|
| 40 |
foreach ( $this->promotions_list as $promotion ) { |
| 41 |
if ( $promotion->get_promotion_name() === $promotion_name ) { |
| 42 |
return $promotion->get_time_interval()->contains( $time ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the list of promotions. |
| 51 |
* |
| 52 |
* @return array<Abstract_Promotion> The list of promotions. |
| 53 |
*/ |
| 54 |
public function get_promotions_list(): array { |
| 55 |
return $this->promotions_list; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get the names of currently active promotions. |
| 60 |
* |
| 61 |
* @return array<string> The list of promotions. |
| 62 |
*/ |
| 63 |
public function get_current_promotions(): array { |
| 64 |
$current_promotions = []; |
| 65 |
$time = \time(); |
| 66 |
foreach ( $this->promotions_list as $promotion ) { |
| 67 |
if ( $promotion->get_time_interval()->contains( $time ) ) { |
| 68 |
$current_promotions[] = $promotion->get_promotion_name(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $current_promotions; |
| 73 |
} |
| 74 |
} |
| 75 |
|