Passed
Push — master ( 8a603c...aaf589 )
by Cesar
12:36 queued 10:34
created

ErrorHandler::catch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Cesargb\Log;
4
5
use Cesargb\Log\Exceptions\RotationFailed;
6
use Throwable;
7
8
trait ErrorHandler
9
{
10
    private $catchCallable = null;
11
12
    private ?string $_filename = null;
13
14
    /**
15
     * Call function if roteted catch any Exception.
16
     */
17
    public function catch(callable $callable): self
18
    {
19
        $this->catchCallable = $callable;
20
21
        return $this;
22
    }
23
24
    protected function setFilename(string $filename): void
25
    {
26
        $this->_filename = $filename;
27
    }
28
29
    protected function exception(Throwable $exception): self
30
    {
31
        if ($this->catchCallable) {
32
            call_user_func($this->catchCallable, $this->convertException($exception));
33
        } else {
34
            throw $this->convertException($exception);
35
        }
36
37
        return $this;
38
    }
39
40
    private function convertException(Throwable $exception): RotationFailed
41
    {
42
        return new RotationFailed(
43
            $exception->getMessage(),
44
            $exception->getCode(),
45
            $exception->getPrevious(),
46
            $this->_filename
47
        );
48
    }
49
}
50