| Total Complexity | 8 |
| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class AuthorizationMiddleware implements Middleware |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | private $expirationPeriod; |
||
| 15 | |||
| 16 | public function __construct($expirationPeriod = '1 day') |
||
| 17 | { |
||
| 18 | $this->expirationPeriod = $expirationPeriod; |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * {@inheritdoc} |
||
| 23 | */ |
||
| 24 | public function execute($command, callable $next) |
||
| 25 | { |
||
| 26 | if (!$command->storage) { |
||
| 27 | throw new \LogicException("Require storage before using this middleware!"); |
||
| 28 | } |
||
| 29 | |||
| 30 | if (!$command->secured) { |
||
| 31 | return $next($command); |
||
| 32 | } |
||
| 33 | |||
| 34 | if (!$this->isSecureExpired($command->storage)) { |
||
| 35 | return $next($command); |
||
| 36 | } |
||
| 37 | |||
| 38 | $cmd = new LoginCommand(); |
||
| 39 | $cmd->input = $command->input; |
||
| 40 | $cmd->storage = $command->storage; |
||
| 41 | $cmd->logs = $command->logs; |
||
| 42 | |||
| 43 | $command['switchTo'] = $cmd; |
||
| 44 | |||
| 45 | return $next($command); |
||
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param User $user |
||
| 51 | * |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | private function isSecureExpired(User $user) |
||
| 66 | } |
||
| 67 | } |
||
| 68 |