Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AccountFilterMiddlewareBase
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 filterCore
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 filter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0
 handleBefore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handleAfter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Middleware;
6
7use PeServer\App\Models\Data\SessionAccount;
8use PeServer\App\Models\SessionKey;
9use PeServer\Core\Http\HttpResponse;
10use PeServer\Core\Http\HttpStatus;
11use PeServer\Core\Mvc\Middleware\IMiddleware;
12use PeServer\Core\Mvc\Middleware\MiddlewareArgument;
13use PeServer\Core\Mvc\Middleware\MiddlewareResult;
14
15abstract class AccountFilterMiddlewareBase implements IMiddleware
16{
17    /**
18     * アカウント用フィルタ処理。
19     *
20     * @param MiddlewareArgument $argument
21     * @param string[] $levels
22     * @return MiddlewareResult
23     */
24    protected function filterCore(MiddlewareArgument $argument, array $levels): MiddlewareResult
25    {
26        if (!$argument->stores->session->tryGet(SessionKey::ACCOUNT, $account)) {
27            return MiddlewareResult::error(HttpStatus::Forbidden);
28        }
29
30        foreach ($levels as $level) {
31            /** @var SessionAccount $account */
32            if ($account->level === $level) {
33                return MiddlewareResult::none();
34            }
35        }
36
37        return MiddlewareResult::error(HttpStatus::Forbidden);
38    }
39
40    abstract protected function filter(MiddlewareArgument $argument): MiddlewareResult;
41
42    //[IMiddleware]
43
44    final public function handleBefore(MiddlewareArgument $argument): MiddlewareResult
45    {
46        return $this->filter($argument);
47    }
48
49    final public function handleAfter(MiddlewareArgument $argument, HttpResponse $response): MiddlewareResult
50    {
51        return MiddlewareResult::none();
52    }
53}