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
|
|
|
|
20
|
|
|
class Manager extends BaseObject implements ManagerInterface |
21
|
|
|
{ |
22
|
|
|
public bool $debug = YII_DEBUG; |
23
|
|
|
|
24
|
|
|
/** @var Middleware[] */ |
25
|
|
|
private array $middlewares = []; |
26
|
|
|
|
27
|
|
|
private StorageInterface $storage; |
28
|
|
|
|
29
|
33 |
|
public function __construct( |
30
|
|
|
StorageInterface $storage, |
31
|
|
|
array $config = [] |
32
|
|
|
) |
33
|
|
|
{ |
34
|
33 |
|
parent::__construct($config); |
35
|
33 |
|
$this->storage = $storage; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array{string, array, Middleware} $middlewares |
|
|
|
|
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
30 |
|
public function setMiddlewares(array $middlewares): void |
43
|
|
|
{ |
44
|
30 |
|
$result = []; |
45
|
30 |
|
foreach ($middlewares as $middleware) { |
46
|
30 |
|
$result[] = $this->createMiddleware($middleware); |
47
|
|
|
} |
48
|
30 |
|
$this->middlewares = $result; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string|array|Middleware $middleware |
53
|
|
|
* @return Middleware |
54
|
|
|
*/ |
55
|
30 |
|
protected function createMiddleware($middleware): Middleware |
56
|
|
|
{ |
57
|
30 |
|
if (is_object($middleware)) { |
58
|
3 |
|
return $middleware; |
59
|
|
|
} |
60
|
|
|
/** @var Middleware */ |
61
|
27 |
|
return Yii::createObject($middleware); |
62
|
|
|
} |
63
|
|
|
|
64
|
27 |
|
public function createMessageBuilder(string $entityName): MessageBuilderInterface |
65
|
|
|
{ |
66
|
27 |
|
$pipeline = new MiddlewarePipeline(...$this->middlewares); |
67
|
27 |
|
$builder = new MessageBuilder($entityName); |
68
|
27 |
|
return $pipeline->handle($builder); |
69
|
|
|
} |
70
|
|
|
|
71
|
28 |
|
public function log(MessageData $message): bool |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
28 |
|
$this->storage->save($message); |
75
|
27 |
|
return true; |
76
|
1 |
|
} catch (Throwable $e) { |
77
|
1 |
|
$this->throwException($e); |
78
|
1 |
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
public function delete(DeleteCommand $command): bool |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
6 |
|
$this->storage->delete($command); |
86
|
5 |
|
return true; |
87
|
2 |
|
} catch (Throwable $e) { |
88
|
2 |
|
$this->throwException($e); |
89
|
1 |
|
return false; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
private function throwException(Throwable $e): void |
94
|
|
|
{ |
95
|
2 |
|
if ($this->debug) { |
96
|
2 |
|
throw $e; |
97
|
|
|
} |
98
|
1 |
|
Yii::error($e->getMessage(), static::class); |
99
|
|
|
} |
100
|
|
|
} |