| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Task driver collector implementation. |
| 16 |
* |
| 17 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 18 |
*/ |
| 19 |
final class VBOTaskDrivercollector |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private $created = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private $modified = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $cancelled = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* Proxy to construct the object. |
| 38 |
* |
| 39 |
* @return VBOTaskDrivercollector |
| 40 |
*/ |
| 41 |
public static function getInstance() |
| 42 |
{ |
| 43 |
return new static(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Class constructor. |
| 48 |
*/ |
| 49 |
public function __construct() |
| 50 |
{ |
| 51 |
$this->reset(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Resets the task collector pools. |
| 56 |
* |
| 57 |
* @return VBOTaskDrivercollector |
| 58 |
*/ |
| 59 |
public function reset() |
| 60 |
{ |
| 61 |
$this->created = []; |
| 62 |
$this->modified = []; |
| 63 |
$this->cancelled = []; |
| 64 |
|
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns a list of the task pools. |
| 70 |
* |
| 71 |
* @param bool $filter If true, only the non-empty pools will be returned. |
| 72 |
* |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function getAll(bool $filter = false) |
| 76 |
{ |
| 77 |
if ($filter) { |
| 78 |
return array_merge( |
| 79 |
array_filter($this->created), |
| 80 |
array_filter($this->modified), |
| 81 |
array_filter($this->cancelled) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
return [ |
| 86 |
$this->created, |
| 87 |
$this->modified, |
| 88 |
$this->cancelled, |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the newly created tasks pool. |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function getCreated() |
| 98 |
{ |
| 99 |
return $this->created; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns the modified tasks pool, which |
| 104 |
* can include cancelled and created tasks. |
| 105 |
* |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public function getModified() |
| 109 |
{ |
| 110 |
if (!$this->modified && ($this->getCreated() || $this->getCancelled())) { |
| 111 |
// some tasks were created or deleted, but not directly modified |
| 112 |
return $this->getAll(true); |
| 113 |
} |
| 114 |
|
| 115 |
return $this->modified; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the cancelled tasks pool. |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function getCancelled() |
| 124 |
{ |
| 125 |
return $this->cancelled; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Registers a new task within the collector pool. |
| 130 |
* |
| 131 |
* @param array $task The task information to register. |
| 132 |
* @param string $type The type of task to register. |
| 133 |
* |
| 134 |
* @return bool |
| 135 |
*/ |
| 136 |
public function register(array $task, string $type = 'created') |
| 137 |
{ |
| 138 |
if (!strcasecmp($type, 'created')) { |
| 139 |
$this->created[] = $task; |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
if (!strcasecmp($type, 'modified')) { |
| 144 |
$this->modified[] = $task; |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
if (!strcasecmp($type, 'cancelled')) { |
| 149 |
$this->cancelled[] = $task; |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
} |
| 156 |
|