Passed
Push — master ( 02fdfb...8dad70 )
by Loban
08:29
created

Manager::createMiddlewares()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @link https://github.com/lav45/yii2-activity-logger
4
 * @copyright Copyright (c) 2017 LAV45
5
 * @author Aleksey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\activityLogger;
10
11
use lav45\activityLogger\middlewares\Middleware;
12
use lav45\activityLogger\middlewares\MiddlewarePipeline;
13
use lav45\activityLogger\storage\DeleteCommand;
14
use lav45\activityLogger\storage\MessageData;
15
use lav45\activityLogger\storage\StorageInterface;
16
use Throwable;
17
use Yii;
18
use yii\base\BaseObject;
19
use yii\di\Instance;
20
21
class Manager extends BaseObject implements ManagerInterface
22
{
23
    public bool $debug = YII_DEBUG;
24
25
    /** @var array{string, array, Middleware} */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{string, array, Middleware} at position 2 could not be parsed: Expected ':' at position 2, but found 'string'.
Loading history...
26
    public array $middlewares = [];
27
28
    private StorageInterface $storage;
29
30 33
    public function __construct(
31
        StorageInterface $storage,
32
        array            $config = []
33
    )
34
    {
35 33
        parent::__construct($config);
36 33
        $this->storage = $storage;
37
    }
38
39
    /**
40
     * @return Middleware[]
41
     */
42 27
    private function createMiddlewares(): array
43
    {
44 27
        $result = [];
45 27
        foreach ($this->middlewares as $middleware) {
46 27
            $result[] = Instance::ensure($middleware, Middleware::class);
47
        }
48 27
        return $result;
49
    }
50
51 27
    public function createMessageBuilder(string $entityName): MessageBuilderInterface
52
    {
53 27
        $middlewares = $this->createMiddlewares();
54 27
        $pipeline = new MiddlewarePipeline(...$middlewares);
55 27
        $builder = new MessageBuilder($entityName);
56 27
        return $pipeline->handle($builder);
57
    }
58
59 28
    public function log(MessageData $message): bool
60
    {
61
        try {
62 28
            $this->storage->save($message);
63 27
            return true;
64 1
        } catch (Throwable $e) {
65 1
            $this->throwException($e);
66 1
            return false;
67
        }
68
    }
69
70 6
    public function delete(DeleteCommand $command): bool
71
    {
72
        try {
73 6
            $this->storage->delete($command);
74 5
            return true;
75 2
        } catch (Throwable $e) {
76 2
            $this->throwException($e);
77 1
            return false;
78
        }
79
    }
80
81 2
    private function throwException(Throwable $e): void
82
    {
83 2
        if ($this->debug) {
84 2
            throw $e;
85
        }
86 1
        Yii::error($e->getMessage(), static::class);
87
    }
88
}