Code Coverage  | 
      ||||||||||
Classes and Traits  | 
       Functions and Methods  | 
       Lines  | 
      ||||||||
| Total |         | 
       0.00%  | 
       0 / 1  | 
               | 
       7.69%  | 
       1 / 13  | 
       CRAP |         | 
       2.33%  | 
       1 / 43  | 
      
| Middleware |         | 
       0.00%  | 
       0 / 1  | 
               | 
       7.69%  | 
       1 / 13  | 
       473.01 |         | 
       2.33%  | 
       1 / 43  | 
      
| __construct |         | 
       0.00%  | 
       0 / 1  | 
       2 |         | 
       0.00%  | 
       0 / 3  | 
      |||
| setInterface |         | 
       0.00%  | 
       0 / 1  | 
       2 |         | 
       0.00%  | 
       0 / 3  | 
      |||
| getInterface |         | 
       0.00%  | 
       0 / 1  | 
       6 |         | 
       0.00%  | 
       0 / 3  | 
      |||
| setMiddlewareData |         | 
       0.00%  | 
       0 / 1  | 
       2 |         | 
       0.00%  | 
       0 / 3  | 
      |||
| name |         | 
       100.00%  | 
       1 / 1  | 
       1 |         | 
       100.00%  | 
       1 / 1  | 
      |||
| info |         | 
       0.00%  | 
       0 / 1  | 
       2 |         | 
       0.00%  | 
       0 / 2  | 
      |||
| error |         | 
       0.00%  | 
       0 / 1  | 
       2 |         | 
       0.00%  | 
       0 / 2  | 
      |||
| getId |         | 
       0.00%  | 
       0 / 1  | 
       12 |         | 
       0.00%  | 
       0 / 5  | 
      |||
| getParentId |         | 
       0.00%  | 
       0 / 1  | 
       12 |         | 
       0.00%  | 
       0 / 5  | 
      |||
| getLog |         | 
       0.00%  | 
       0 / 1  | 
       2 |         | 
       0.00%  | 
       0 / 1  | 
      |||
| getError |         | 
       0.00%  | 
       0 / 1  | 
       2 |         | 
       0.00%  | 
       0 / 1  | 
      |||
| getWarning |         | 
       0.00%  | 
       0 / 1  | 
       2 |         | 
       0.00%  | 
       0 / 1  | 
      |||
| commandInformations |         | 
       0.00%  | 
       0 / 1  | 
       30 |         | 
       0.00%  | 
       0 / 13  | 
      |||
| 1 | <?php | 
| 2 | |
| 3 | namespace Qmp\Laravel\CommandsLaravel\Middleware\Core; | 
| 4 | |
| 5 | use Exception; | 
| 6 | use Illuminate\Support\Facades\App; | 
| 7 | use Qmp\Laravel\CommandsLaravel\Console\Commands\Command; | 
| 8 | use Qmp\Laravel\CommandsLaravel\Middleware\Core\Interfaces\CommandMiddlewareInterface; | 
| 9 | use Qmp\Laravel\CommandsLaravel\Middleware\Core\Interfaces\IOConnectorInterface; | 
| 10 | |
| 11 | abstract class Middleware implements CommandMiddlewareInterface | 
| 12 | { | 
| 13 | /** | 
| 14 | * | 
| 15 | */ | 
| 16 | const IS_SYSTEM = ['IS_SYSTEM' => 1]; | 
| 17 | |
| 18 | /** | 
| 19 | * The middleware name | 
| 20 | * | 
| 21 | * @var string | 
| 22 | */ | 
| 23 | protected $name; | 
| 24 | |
| 25 | /** | 
| 26 | * The command instance | 
| 27 | * | 
| 28 | * @var IOConnectorInterface | 
| 29 | */ | 
| 30 | protected $interface; | 
| 31 | |
| 32 | /** | 
| 33 | * The middlewarData instances | 
| 34 | * | 
| 35 | * @var MiddlewareData | 
| 36 | */ | 
| 37 | protected $middlewareData; | 
| 38 | |
| 39 | |
| 40 | /** | 
| 41 | * Undocumented variable | 
| 42 | * | 
| 43 | * @var stdClass | 
| 44 | */ | 
| 45 | protected $data; | 
| 46 | |
| 47 | /** | 
| 48 | * Used in id() | 
| 49 | * | 
| 50 | * @var string | 
| 51 | */ | 
| 52 | protected $idArgument; | 
| 53 | |
| 54 | /** | 
| 55 | * Used in parentId() | 
| 56 | * | 
| 57 | * @var string | 
| 58 | */ | 
| 59 | protected $parentIdArgument; | 
| 60 | |
| 61 | /** | 
| 62 | * Undocumented variable | 
| 63 | * | 
| 64 | * @var string | 
| 65 | */ | 
| 66 | protected $serviceName; | 
| 67 | |
| 68 | /** | 
| 69 | * Undocumented variable | 
| 70 | * | 
| 71 | * @var string | 
| 72 | */ | 
| 73 | protected $messagePrefix = "[Middleware::%s] %s"; | 
| 74 | |
| 75 | /** | 
| 76 | *Constructor | 
| 77 | * | 
| 78 | * @param Command|null $command | 
| 79 | */ | 
| 80 | public function __construct() | 
| 81 | { | 
| 82 | $this->name = self::name(); | 
| 83 | $this->serviceName = str_replace("-", "_", explode('-laravel', config('micro-services.name'))[0]); | 
| 84 | } | 
| 85 | |
| 86 | /** | 
| 87 | * Undocumented function | 
| 88 | * | 
| 89 | * @param IOConnectorInterface $interface | 
| 90 | * @return self | 
| 91 | */ | 
| 92 | protected function setInterface(IOConnectorInterface $interface): self { | 
| 93 | $this->interface = $interface; | 
| 94 | $this->setMiddlewareData($this->interface->getMiddlewareData()); | 
| 95 | return $this; | 
| 96 | } | 
| 97 | |
| 98 | /** | 
| 99 | * Undocumented function | 
| 100 | * | 
| 101 | * @return IOConnectorInterface | 
| 102 | */ | 
| 103 | protected function getInterface(): IOConnectorInterface { | 
| 104 | if(isset($this->interface)) { | 
| 105 | return $this->interface; | 
| 106 | } | 
| 107 | |
| 108 | throw new Exception('No IOConnectorInterface found !'); | 
| 109 | } | 
| 110 | |
| 111 | /** | 
| 112 | * Undocumented function | 
| 113 | * | 
| 114 | * @param MiddlewareData $data | 
| 115 | * @return self | 
| 116 | */ | 
| 117 | protected function setMiddlewareData(MiddlewareData $data) { | 
| 118 | $this->middlewareData = $data; | 
| 119 | $this->data = $data->prepare(get_called_class())->get(); | 
| 120 | return $this; | 
| 121 | } | 
| 122 | |
| 123 | /** | 
| 124 | * Return the name of the current middleware | 
| 125 | * | 
| 126 | * @return string | 
| 127 | */ | 
| 128 | public static function name() | 
| 129 | { | 
| 130 | return class_basename(get_called_class()); | 
| 131 | } | 
| 132 | |
| 133 | /** | 
| 134 | * Output info text to the console | 
| 135 | * | 
| 136 | * @param string|iterable $message | 
| 137 | * @return void | 
| 138 | */ | 
| 139 | protected function info($message) | 
| 140 | { | 
| 141 | $this->interface->line(sprintf($this->messagePrefix, self::name(), $message), 'info'); | 
| 142 | } | 
| 143 | |
| 144 | /** | 
| 145 | * Output error text to the console | 
| 146 | * | 
| 147 | * @param string|iterable $message | 
| 148 | * @return void | 
| 149 | */ | 
| 150 | protected function error($message) | 
| 151 | { | 
| 152 | $this->interface->line(sprintf($this->messagePrefix, self::name(), $message), 'error'); | 
| 153 | } | 
| 154 | |
| 155 | /** | 
| 156 | * Return the id stored in data | 
| 157 | * or setted with $idArgument | 
| 158 | * | 
| 159 | * @return string|null | 
| 160 | */ | 
| 161 | protected function getId() | 
| 162 | { | 
| 163 | if (isset($this->data->id)) { | 
| 164 | return $this->data->id; | 
| 165 | } | 
| 166 | |
| 167 | if (isset($this->idArgument)) { | 
| 168 | return $this->interface->argument($this->idArgument); | 
| 169 | } | 
| 170 | |
| 171 | return null; | 
| 172 | } | 
| 173 | |
| 174 | /** | 
| 175 | * Return the parent id stored in data | 
| 176 | * or setted with $parentIdArgument | 
| 177 | * | 
| 178 | * @return string|null | 
| 179 | */ | 
| 180 | protected function getParentId() | 
| 181 | { | 
| 182 | if (isset($this->data->parentId)) { | 
| 183 | return $this->data->parentId; | 
| 184 | } | 
| 185 | |
| 186 | if (isset($this->parentIdArgument)) { | 
| 187 | return $this->interface->argument($this->parentIdArgument); | 
| 188 | } | 
| 189 | |
| 190 | return null; | 
| 191 | } | 
| 192 | |
| 193 | /** | 
| 194 | * Return log key from $this->data or null | 
| 195 | * | 
| 196 | * @return mixed | 
| 197 | */ | 
| 198 | protected function getLog() | 
| 199 | { | 
| 200 | return $this->data->log ?? null; | 
| 201 | } | 
| 202 | |
| 203 | /** | 
| 204 | * Return error key from $this->data or null | 
| 205 | * | 
| 206 | * @return mixed | 
| 207 | */ | 
| 208 | protected function getError() | 
| 209 | { | 
| 210 | return $this->data->error ?? null; | 
| 211 | } | 
| 212 | |
| 213 | /** | 
| 214 | * Return log warning from $this->data or null | 
| 215 | * | 
| 216 | * @return mixed | 
| 217 | */ | 
| 218 | protected function getWarning() | 
| 219 | { | 
| 220 | return $this->data->warning ?? null; | 
| 221 | } | 
| 222 | |
| 223 | /** | 
| 224 | * Get all the command infomartion, return an array with | 
| 225 | * command type, command name and arguments/options | 
| 226 | * | 
| 227 | * @return array | 
| 228 | */ | 
| 229 | protected function commandInformations($commandType = null): array | 
| 230 | { | 
| 231 | $commandType = $commandType ?? | 
| 232 | env('IS_RUNNER', null) == 1 | 
| 233 | ? 'runner' | 
| 234 | : (env('IS_CRON', null) == 1 | 
| 235 | ? 'cron' | 
| 236 | : (env('IS_SYSTEM', null) == 1 | 
| 237 | ? 'system' | 
| 238 | : (App::runningInConsole() | 
| 239 | ? 'manuel (cli)' | 
| 240 | : 'manuel (interface)'))); | 
| 241 | |
| 242 | return [ | 
| 243 | $commandType, | 
| 244 | $this->interface->getName(), | 
| 245 | $this->interface->getArgs() | 
| 246 | ]; | 
| 247 | } | 
| 248 | } |