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

ErrorHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setFilename() 0 3 1
A exception() 0 9 2
A catch() 0 5 1
A convertException() 0 7 1
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