Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
73.91% |
51 / 69 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
Log | |
73.91% |
51 / 69 |
|
42.86% |
3 / 7 |
20.54 | |
0.00% |
0 / 1 |
writeLogEntry | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
writeProcessLog | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
6 | |||
readByProcessId | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
readByProcessData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
delete | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
backtraceLogEntry | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 | |||
clearDataOlderThan | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Collection\RequestList; |
6 | use BO\Zmsentities\Log as Entity; |
7 | |
8 | /** |
9 | * Logging for actions |
10 | * |
11 | */ |
12 | class Log extends Base |
13 | { |
14 | const PROCESS = 'buerger'; |
15 | const MIGRATION = 'migration'; |
16 | const ERROR = 'error'; |
17 | |
18 | const ACTION_MAIL_SUCCESS = 'E-Mail-Versand erfolgreich'; |
19 | const ACTION_MAIL_FAIL = 'E-Mail-Versand ist fehlgeschlagen'; |
20 | const ACTION_STATUS_CHANGE = 'Terminstatus wurde geändert'; |
21 | const ACTION_SEND_REMINDER = 'Erinnerungsmail wurde gesendet'; |
22 | const ACTION_REMOVED = 'Termin aus der Warteschlange entfernt'; |
23 | const ACTION_CALLED = 'Termin wurde aufgerufen'; |
24 | const ACTION_ARCHIVED = 'Termin wurde archiviert'; |
25 | const ACTION_EDITED = 'Termin wurde geändert'; |
26 | const ACTION_NEW_PICKUP = 'Abholtermin wurde erstellt'; |
27 | const ACTION_REDIRECTED = 'Termin wurde weitergeleitet'; |
28 | const ACTION_NEW = 'Neuer Termin wurde erstellt'; |
29 | const ACTION_DELETED = 'Termin wurde gelöscht'; |
30 | const ACTION_CANCELED = 'Termin wurde abgesagt'; |
31 | |
32 | public static $operator = 'lib'; |
33 | |
34 | public static function writeLogEntry( |
35 | $message, |
36 | $referenceId, |
37 | $type = self::PROCESS, |
38 | ?int $scopeId = null, |
39 | ?string $userId = null, |
40 | ?string $data = null |
41 | ) { |
42 | $message .= " [" . static::$operator . "]"; |
43 | $log = new static(); |
44 | $sql = "INSERT INTO `log` SET |
45 | `message`=:message, |
46 | `reference_id`=:referenceId, |
47 | `type`=:type, |
48 | `scope_id`=:scopeId, |
49 | `user_id`=:userId, |
50 | `data`=:dataString"; |
51 | |
52 | $parameters = [ |
53 | "message" => $message . static::backtraceLogEntry(), |
54 | "referenceId" => $referenceId, |
55 | "type" => $type, |
56 | "scopeId" => $scopeId, |
57 | "userId" => $userId, |
58 | "dataString" => $data |
59 | ]; |
60 | |
61 | return $log->perform($sql, $parameters); |
62 | } |
63 | |
64 | public static function writeProcessLog( |
65 | string $method, |
66 | string $action, |
67 | ?\BO\Zmsentities\Process $process, |
68 | ?\BO\Zmsentities\Useraccount $userAccount = null |
69 | ) { |
70 | if (empty($process) || empty($process->getId()) || empty($userAccount)) { |
71 | return; |
72 | } |
73 | |
74 | $requests = new RequestList(); |
75 | if (! empty($process->getRequestIds())) { |
76 | $requests = (new Request())->readRequestsByIds($process->getRequestIds()); |
77 | } |
78 | |
79 | $data = json_encode(array_filter([ |
80 | 'Aktion' => $action, |
81 | "Sachbearbeiter*in" => $userAccount ? $userAccount->getId() : '', |
82 | "Terminnummer" => $process->getId(), |
83 | "Terminzeit" => $process->getFirstAppointment()->toDateTime()->format('d.m.Y H:i:s'), |
84 | "Bürger*in" => $process->getFirstClient()->familyName, |
85 | "Dienstleistung/en" => implode(', ', array_map(function ($request) { |
86 | return $request->getName(); |
87 | }, $requests->getAsArray())), |
88 | "Anmerkung" => $process->getAmendment(), |
89 | "E-Mail" => $process->getFirstClient()->email, |
90 | "Telefon" => $process->getFirstClient()->telephone, |
91 | ]), JSON_UNESCAPED_UNICODE); |
92 | |
93 | Log::writeLogEntry( |
94 | $method, |
95 | $process->getId(), |
96 | self::PROCESS, |
97 | $process->getScopeId(), |
98 | $userAccount->getId(), |
99 | $data |
100 | ); |
101 | } |
102 | |
103 | public function readByProcessId($processId) |
104 | { |
105 | $query = new Query\Log(Query\Base::SELECT); |
106 | $query->addEntityMapping(); |
107 | $query->addConditionProcessId($processId); |
108 | $logList = new \BO\Zmsentities\Collection\LogList($this->fetchList($query, new Entity())); |
109 | return $logList; |
110 | } |
111 | |
112 | public function readByProcessData($search) |
113 | { |
114 | $query = new Query\Log(Query\Base::SELECT); |
115 | $query->addEntityMapping(); |
116 | $query->addConditionDataSearch($search); |
117 | $query->addLimit(1000); |
118 | |
119 | return new \BO\Zmsentities\Collection\LogList($this->fetchList($query, new Entity())); |
120 | } |
121 | |
122 | public function delete($processId) |
123 | { |
124 | $query = new Query\Log(Query\Base::SELECT); |
125 | $query->addEntityMapping(); |
126 | $query->addConditionProcessId($processId); |
127 | $logList = new \BO\Zmsentities\Collection\LogList($this->fetchList($query, new Entity())); |
128 | return $logList; |
129 | } |
130 | |
131 | protected static function backtraceLogEntry() |
132 | { |
133 | $trace = debug_backtrace(); |
134 | $short = ''; |
135 | foreach ($trace as $step) { |
136 | if ( |
137 | isset($step['file']) |
138 | && isset($step['line']) |
139 | && !strpos($step['file'], 'Zmsdb') |
140 | ) { |
141 | return ' (' . basename($step['file'], '.php') . ')'; |
142 | } |
143 | } |
144 | return $short; |
145 | } |
146 | |
147 | public function clearDataOlderThan(int $olderThan) |
148 | { |
149 | $olderThanDate = (new \DateTime())->modify('-' . $olderThan . ' days'); |
150 | |
151 | $query = new Query\Log(Query\Base::UPDATE); |
152 | $query->addConditionOlderThan($olderThanDate); |
153 | $query->addValues([ |
154 | 'data' => null |
155 | ]); |
156 | |
157 | $this->writeItem($query); |
158 | } |
159 | } |