HandleExceptionsMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\RuntimeError;
9
use hiapi\exceptions\domain\DomainException;
10
use hiapi\exceptions\NotAuthenticatedException;
11
use League\Tactician\Middleware;
12
use Psr\Log\LoggerInterface;
13
use hiapi\exceptions\SystemError;
14
15
/**
16
 * Class HandleExceptionsMiddleware
17
 *
18
 * @author Dmytro Naumenko <[email protected]>
19
 */
20
class HandleExceptionsMiddleware implements Middleware
21
{
22
    /**
23
     * @var LoggerInterface
24
     */
25
    private $logger;
26
27
    public function __construct(LoggerInterface $logger)
28
    {
29
        $this->logger = $logger;
30
    }
31
32
    /**
33
     * @var bool
34
     */
35
    protected $keepSystemErrorMessage = false;
36
37
    /**
38
     * @param object|BaseCommand $command
39
     * @param callable $next
40
     *
41
     * @return mixed
42
     */
43
    public function execute($command, callable $next)
44
    {
45
        try {
46
            return $next($command);
47
        } catch (NotAuthenticatedException $exception) {
48
            return $this->handleAuthenticationError($command, $exception);
49
        } catch (DomainException|\DomainException $domainException) {
50
            return $this->handleDomainException($command, $domainException);
51
        } catch (\InvalidArgumentException $argumentException) {
52
            return $this->handleArgumentException($command, $argumentException);
53
        } catch (\Exception $exception) {
54
            return $this->handleUnclassifiedError($command, $exception);
55
        }
56
    }
57
58
    public function setKeepSystemErrorMessage(bool $keepSystemErrorMessage): void
59
    {
60
        $this->keepSystemErrorMessage = $keepSystemErrorMessage;
61
    }
62
63
    private function handleDomainException(BaseCommand $command, \Exception $domainException)
64
    {
65
        return new LogicalError($command, $domainException);
66
    }
67
68
    private function handleArgumentException(BaseCommand $command, \InvalidArgumentException $exception)
69
    {
70
        return new RuntimeError($command, $this->ensureExceptionCanBeKept($exception));
71
    }
72
73
    private function handleUnclassifiedError(BaseCommand $command, \Exception $exception)
74
    {
75
        return new RuntimeError($command, $this->ensureExceptionCanBeKept($exception));
76
    }
77
78
    private function handleAuthenticationError(BaseCommand $command, NotAuthenticatedException $exception)
79
    {
80
        return new AuthenticationError($command, $exception);
81
    }
82
83
    private function ensureExceptionCanBeKept(\Exception $exception): \Exception
84
    {
85
        $this->logger->warning($exception->getMessage(), ['exception' => $exception]);
86
87
        if (!$this->keepSystemErrorMessage) {
88
            return new SystemError('From HandleExceptionsMiddleware', $exception, $exception->getCode());
89
        }
90
91
        return $exception;
92
    }
93
}
94