Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
LoggerService | |
0.00% |
0 / 40 |
|
0.00% |
0 / 9 |
210 | |
0.00% |
0 / 1 |
getAction | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
setAction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
log | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
logEvent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
rotate | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
searchInstances | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getStorage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getUser | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
createEventLogEntity | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * @author Alexander Zagovorichev <zagovorichev@1pt.com> |
21 | */ |
22 | |
23 | namespace oat\taoEventLog\model\eventLog; |
24 | |
25 | use common_exception_Error; |
26 | use common_session_Session; |
27 | use common_session_SessionManager; |
28 | use Context; |
29 | use DateTimeImmutable; |
30 | use DateTimeZone; |
31 | use JsonSerializable; |
32 | use oat\dtms\DateInterval; |
33 | use oat\oatbox\event\BulkEvent; |
34 | use oat\oatbox\event\Event; |
35 | use oat\oatbox\service\ServiceManager; |
36 | use oat\oatbox\user\User; |
37 | use oat\taoEventLog\model\storage\RdsStorage as DeprecatedRdsStorage; |
38 | use oat\dtms\DateTime; |
39 | use oat\taoEventLog\model\AbstractLog; |
40 | use oat\taoEventLog\model\StorageInterface; |
41 | |
42 | /** |
43 | * Class LoggerService |
44 | * @package oat\taoEventLog\model\eventLog |
45 | */ |
46 | class LoggerService extends AbstractLog |
47 | { |
48 | public const SERVICE_ID = 'taoEventLog/eventLogger'; |
49 | |
50 | public const OPTION_ROTATION_PERIOD = 'rotation_period'; |
51 | public const OPTION_EXPORTABLE_QUANTITY = 'exportable_quantity'; |
52 | public const OPTION_FETCH_LIMIT = 'fetch_limit'; |
53 | |
54 | /** |
55 | * @var string |
56 | */ |
57 | private $action = ''; |
58 | |
59 | public function getAction() |
60 | { |
61 | if (!$this->action) { |
62 | $this->action = 'cli' === php_sapi_name() |
63 | ? $_SERVER['PHP_SELF'] |
64 | : Context::getInstance()->getRequest()->getRequestURI(); |
65 | } |
66 | return $this->action; |
67 | } |
68 | |
69 | public function setAction($action = '') |
70 | { |
71 | $this->action = $action; |
72 | } |
73 | |
74 | /** |
75 | * @param Event $event |
76 | * @throws common_exception_Error |
77 | */ |
78 | public function log(Event $event) |
79 | { |
80 | $currentUser = $this->getUser(); |
81 | |
82 | try { |
83 | if ($event instanceof BulkEvent) { |
84 | $this->getStorage()->logMultiple( |
85 | ...array_map( |
86 | fn (array $eventData): EventLogEntity => $this->createEventLogEntity( |
87 | $event, |
88 | $currentUser, |
89 | $eventData |
90 | ), |
91 | $event->getValues() |
92 | ) |
93 | ); |
94 | |
95 | return; |
96 | } |
97 | |
98 | $data = is_subclass_of($event, JsonSerializable::class) ? $event : []; |
99 | $this->getStorage()->log($this->createEventLogEntity($event, $currentUser, $data)); |
100 | } catch (\Exception $e) { |
101 | \common_Logger::e('Error logging to DB ' . $e->getMessage()); |
102 | } |
103 | } |
104 | |
105 | /** |
106 | * @deprecated use $this->log() |
107 | * @param Event $event |
108 | */ |
109 | public static function logEvent(Event $event) |
110 | { |
111 | ServiceManager::getServiceManager()->get(self::SERVICE_ID)->log($event); |
112 | } |
113 | |
114 | /** |
115 | * @return mixed |
116 | */ |
117 | public function rotate() |
118 | { |
119 | $period = new DateInterval($this->getOption(self::OPTION_ROTATION_PERIOD)); |
120 | $beforeDate = (new DateTimeImmutable())->sub($period); |
121 | |
122 | return $this->delete([], $beforeDate); |
123 | } |
124 | |
125 | /** |
126 | * @param array $filters |
127 | * @param array $options |
128 | * @deprecated use LoggerService::search() instead |
129 | * @return array |
130 | */ |
131 | public function searchInstances(array $filters = [], array $options = []) |
132 | { |
133 | return $this->search($filters, $options); |
134 | } |
135 | |
136 | /** |
137 | * @return DeprecatedRdsStorage|StorageInterface |
138 | */ |
139 | protected function getStorage() |
140 | { |
141 | $storage = $this->getServiceManager()->get(self::SERVICE_ID)->getOption(self::OPTION_STORAGE); |
142 | return $this->getServiceManager()->get($storage); |
143 | } |
144 | |
145 | private function getUser(): User |
146 | { |
147 | /** @var common_session_Session $session */ |
148 | $session = common_session_SessionManager::getSession(); |
149 | |
150 | return $session->getUser(); |
151 | } |
152 | |
153 | private function createEventLogEntity(Event $event, User $user, $data): EventLogEntity |
154 | { |
155 | return new EventLogEntity( |
156 | $event, |
157 | $this->getAction(), |
158 | $user, |
159 | (new DateTime('now', new DateTimeZone('UTC'))), |
160 | $data |
161 | ); |
162 | } |
163 | } |