Completed
Push — master ( 3c1d03...7e0a80 )
by Dmitry
14:29
created

ensureExceptionCanBeKept()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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