Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| RequestLogService | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| log | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| find | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| count | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getStorage | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| catchEvent | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoEventLog\model\requestLog; |
| 24 | |
| 25 | use GuzzleHttp\Psr7\ServerRequest; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\oatbox\event\Event; |
| 28 | use Psr\Http\Message\RequestInterface; |
| 29 | use oat\tao\model\event\BeforeAction; |
| 30 | |
| 31 | /** |
| 32 | * Class RequestLogService |
| 33 | * @package oat\taoEventLog\model\requestLog |
| 34 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 35 | */ |
| 36 | class RequestLogService extends ConfigurableService |
| 37 | { |
| 38 | public const SERVICE_ID = 'taoEventLog/RequestLogStorage'; |
| 39 | |
| 40 | public const OPTION_STORAGE = 'storage'; |
| 41 | public const OPTION_STORAGE_PARAMETERS = 'storage_parameters'; |
| 42 | |
| 43 | public const USER_ID = 'user_id'; |
| 44 | public const USER_ROLES = 'user_role'; |
| 45 | public const ACTION = 'action'; |
| 46 | public const EVENT_TIME = 'event_time'; |
| 47 | public const DETAILS = 'details'; |
| 48 | |
| 49 | /** @var bool whether request has been already logged during current php process */ |
| 50 | private $fulfilled = false; |
| 51 | |
| 52 | /** @var RequestLogStorageReadable|RequestLogStorageWritable */ |
| 53 | private $storage; |
| 54 | |
| 55 | /** |
| 56 | * @see \oat\taoEventLog\model\requestLog\RequestLogStorageWritable::log |
| 57 | * |
| 58 | * @param RequestInterface|null $request |
| 59 | * @param User|null $user |
| 60 | * @return boolean |
| 61 | * @throws \common_exception_Error |
| 62 | * @throws RequestLogException |
| 63 | */ |
| 64 | public function log(RequestInterface $request = null, User $user = null) |
| 65 | { |
| 66 | if ($request === null) { |
| 67 | $request = ServerRequest::fromGlobals(); |
| 68 | } |
| 69 | |
| 70 | if ($user === null) { |
| 71 | $user = \common_session_SessionManager::getSession()->getUser(); |
| 72 | } |
| 73 | return $this->getStorage()->log($request, $user); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @see \oat\taoEventLog\model\requestLog\RequestLogStorageReadable::find() |
| 78 | * @param array $filters |
| 79 | * @param array $options |
| 80 | * @return \Iterator |
| 81 | * @throws RequestLogException |
| 82 | */ |
| 83 | public function find(array $filters = [], array $options = []) |
| 84 | { |
| 85 | if (!$this->getStorage() instanceof RequestLogStorageReadable) { |
| 86 | throw new RequestLogException('Request log storage is not readable'); |
| 87 | } |
| 88 | return $this->getStorage()->find($filters, $options); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @see \oat\taoEventLog\model\requestLog\RequestLogStorageReadable::count() |
| 93 | * @param array $filters |
| 94 | * @param array $options |
| 95 | * @return integer |
| 96 | * @throws RequestLogException |
| 97 | */ |
| 98 | public function count(array $filters = [], array $options = []) |
| 99 | { |
| 100 | if (!$this->getStorage() instanceof RequestLogStorageReadable) { |
| 101 | throw new RequestLogException('Request log storage is not readable'); |
| 102 | } |
| 103 | return $this->getStorage()->count($filters, $options); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return RequestLogStorageReadable|RequestLogStorageWritable |
| 108 | * @throws |
| 109 | */ |
| 110 | protected function getStorage() |
| 111 | { |
| 112 | if ($this->storage === null) { |
| 113 | $storageClass = $this->getOption(self::OPTION_STORAGE); |
| 114 | if (!class_exists($storageClass)) { |
| 115 | throw new RequestLogException('Storage class does not exist'); |
| 116 | } |
| 117 | $storageParams = $this->getOption(self::OPTION_STORAGE_PARAMETERS) ?: []; |
| 118 | $this->storage = new $storageClass($storageParams); |
| 119 | $this->getServiceManager()->propagate($this->storage); |
| 120 | } |
| 121 | return $this->storage; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param Event $event |
| 126 | * @throws |
| 127 | */ |
| 128 | public function catchEvent(Event $event) |
| 129 | { |
| 130 | if ($event instanceof BeforeAction && $this->fulfilled) { |
| 131 | return; |
| 132 | } |
| 133 | $this->fulfilled = true; |
| 134 | $this->log(); |
| 135 | } |
| 136 | } |