Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.48% |
19 / 21 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Redirect | |
90.48% |
19 / 21 |
|
87.50% |
7 / 8 |
10.09 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setDelay | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUri | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| serialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unserialize | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
3.14 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Model\Message |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Model\Message; |
| 16 | |
| 17 | use phpOMS\Contract\SerializableInterface; |
| 18 | |
| 19 | /** |
| 20 | * Redirect class. |
| 21 | * |
| 22 | * @package phpOMS\Model\Message |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | final class Redirect implements \JsonSerializable, SerializableInterface |
| 28 | { |
| 29 | /** |
| 30 | * Message type. |
| 31 | * |
| 32 | * @var string |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | public const TYPE = 'redirect'; |
| 36 | |
| 37 | /** |
| 38 | * Redirect uri. |
| 39 | * |
| 40 | * @var string |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | private string $uri = ''; |
| 44 | |
| 45 | /** |
| 46 | * Delay. |
| 47 | * |
| 48 | * @var int |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | private int $delay = 0; |
| 52 | |
| 53 | /** |
| 54 | * Window. |
| 55 | * |
| 56 | * @var bool |
| 57 | * @since 1.0.0 |
| 58 | */ |
| 59 | private bool $new = false; |
| 60 | |
| 61 | /** |
| 62 | * Constructor. |
| 63 | * |
| 64 | * @param string $url Url |
| 65 | * @param bool $blank New window |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | public function __construct(string $url = '', bool $blank = false) |
| 70 | { |
| 71 | $this->uri = $url; |
| 72 | $this->new = $blank; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Set delay. |
| 77 | * |
| 78 | * @param int $delay Delay in ms |
| 79 | * |
| 80 | * @return void |
| 81 | * |
| 82 | * @since 1.0.0 |
| 83 | */ |
| 84 | public function setDelay(int $delay) : void |
| 85 | { |
| 86 | $this->delay = $delay; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Set uri. |
| 91 | * |
| 92 | * @param string $uri Uri |
| 93 | * |
| 94 | * @return void |
| 95 | * |
| 96 | * @since 1.0.0 |
| 97 | */ |
| 98 | public function setUri(string $uri) : void |
| 99 | { |
| 100 | $this->uri = $uri; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Render message. |
| 105 | * |
| 106 | * @return string |
| 107 | * |
| 108 | * @since 1.0.0 |
| 109 | */ |
| 110 | public function serialize() : string |
| 111 | { |
| 112 | return $this->__toString(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * {@inheritdoc} |
| 117 | */ |
| 118 | public function jsonSerialize() : mixed |
| 119 | { |
| 120 | return $this->toArray(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * {@inheritdoc} |
| 125 | */ |
| 126 | public function unserialize(mixed $raw) : void |
| 127 | { |
| 128 | if (!\is_string($raw)) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | $unserialized = \json_decode($raw, true); |
| 133 | if (!\is_array($unserialized)) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | $this->delay = $unserialized['time'] ?? 0; |
| 138 | $this->uri = $unserialized['uri'] ?? ''; |
| 139 | $this->new = $unserialized['new'] ?? false; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Stringify. |
| 144 | * |
| 145 | * @return string |
| 146 | * |
| 147 | * @since 1.0.0 |
| 148 | */ |
| 149 | public function __toString() |
| 150 | { |
| 151 | return (string) \json_encode($this->toArray()); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Generate message array. |
| 156 | * |
| 157 | * @return array<string, mixed> |
| 158 | * |
| 159 | * @since 1.0.0 |
| 160 | */ |
| 161 | public function toArray() : array |
| 162 | { |
| 163 | return [ |
| 164 | 'type' => self::TYPE, |
| 165 | 'time' => $this->delay, |
| 166 | 'uri' => $this->uri, |
| 167 | 'new' => $this->new, |
| 168 | ]; |
| 169 | } |
| 170 | } |