Completed
Push — master ( 932cc5...b796f0 )
by Andrii
12:16
created

handleAuthenticationError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace hiapi\middlewares;
4
5
use hiapi\commands\BaseCommand;
6
use hiapi\commands\error\AuthenticationError;
7
use hiapi\commands\error\LogicalError;
8
use hiapi\commands\error\CommandError;
9
use hiapi\commands\error\RuntimeError;
10
use hiapi\exceptions\domain\DomainException;
11
use hiapi\exceptions\NotAuthenticatedException;
12
use League\Tactician\Middleware;
13
use yii\web\HttpException;
14
15
/**
16
 * Class HandleExceptionsMiddleware
17
 *
18
 * @author Dmytro Naumenko <[email protected]>
19
 */
20
class HandleExceptionsMiddleware implements Middleware
21
{
22
    /**
23
     * @param object|BaseCommand $command
24
     * @param callable $next
25
     *
26
     * @return mixed
27
     */
28
    public function execute($command, callable $next)
29
    {
30
        try {
31
            return $next($command);
32
        } catch (NotAuthenticatedException $exception) {
33
            return $this->handleAuthenticationError($command, $exception);
34
        } catch (DomainException $domainException) {
35
            return $this->handleDomainException($command, $domainException);
36
        } catch (\InvalidArgumentException $argumentException) {
37
            return $this->handleArgumentException($command, $argumentException);
38
        } catch (\Exception $exception) {
39
            return $this->handleUnclassifiedError($command, $exception);
40
        }
41
    }
42
43
    private function handleDomainException(BaseCommand $command, DomainException $domainException)
44
    {
45
        return new LogicalError($command, $domainException);
46
    }
47
48
    private function handleArgumentException(BaseCommand $command, \InvalidArgumentException $argumentException)
49
    {
50
        return new LogicalError($command, $argumentException);
51
    }
52
53
    private function handleUnclassifiedError(BaseCommand $command, \Exception $exception)
54
    {
55
        return new RuntimeError($command, $exception);
56
    }
57
58
    private function handleAuthenticationError(BaseCommand $command, NotAuthenticatedException $exception)
59
    {
60
        return new AuthenticationError($command, $exception);
61
    }
62
}
63