Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| MaintenanceTrait | |
100.00% |
12 / 12 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| setMaintenanceAction | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getMaintenanceAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setBypassMaintenance | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| bypassMaintenance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| dispatchMaintenance | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Dispatch; |
| 16 | |
| 17 | /** |
| 18 | * Maintenance trait |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop |
| 22 | * @author Nick Sagona, III <nick@popphp.org> |
| 23 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | trait MaintenanceTrait |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Maintenance action |
| 32 | * @var string |
| 33 | */ |
| 34 | protected string $maintenanceAction = 'maintenance'; |
| 35 | |
| 36 | /** |
| 37 | * Bypass maintenance false |
| 38 | * @var bool |
| 39 | */ |
| 40 | protected bool $bypassMaintenance = false; |
| 41 | |
| 42 | /** |
| 43 | * Set the maintenance action |
| 44 | * |
| 45 | * @param string $maintenance |
| 46 | * @return static |
| 47 | */ |
| 48 | public function setMaintenanceAction(string $maintenance): static |
| 49 | { |
| 50 | $this->maintenanceAction = $maintenance; |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the maintenance action |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public function getMaintenanceAction(): string |
| 60 | { |
| 61 | return $this->maintenanceAction; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check the bypass maintenance check |
| 66 | * |
| 67 | * @param bool $bypass |
| 68 | * @return static |
| 69 | */ |
| 70 | public function setBypassMaintenance(bool $bypass = true): static |
| 71 | { |
| 72 | $this->bypassMaintenance = $bypass; |
| 73 | return $this; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Check the bypass maintenance check |
| 78 | * |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function bypassMaintenance(): bool |
| 82 | { |
| 83 | return $this->bypassMaintenance; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Dispatch the maintenance action |
| 88 | * |
| 89 | * @throws Exception |
| 90 | * @return void |
| 91 | */ |
| 92 | public function dispatchMaintenance(): void |
| 93 | { |
| 94 | if (method_exists($this, $this->maintenanceAction)) { |
| 95 | $action = $this->maintenanceAction; |
| 96 | $this->$action(); |
| 97 | } else { |
| 98 | throw new Exception( |
| 99 | "The application is currently in maintenance mode. The maintenance action is not defined." |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | } |