*/ class RequestLogService extends ConfigurableService { const SERVICE_ID = 'taoEventLog/RequestLogStorage'; const OPTION_STORAGE = 'storage'; const OPTION_STORAGE_PARAMETERS = 'storage_parameters'; const USER_ID = 'user_id'; const USER_ROLES = 'user_role'; const ACTION = 'action'; const EVENT_TIME = 'event_time'; const DETAILS = 'details'; /** @var bool whether request has been already logged during current php process */ private $fulfilled = false; /** @var RequestLogStorageReadable|RequestLogStorageWritable */ private $storage; /** * @see \oat\taoEventLog\model\requestLog\RequestLogStorageWritable::log * * @param RequestInterface|null $request * @param User|null $user * @return boolean * @throws \common_exception_Error * @throws RequestLogException */ public function log(RequestInterface $request = null, User $user = null) { if ($request === null) { $request = ServerRequest::fromGlobals(); } if ($user === null) { $user = \common_session_SessionManager::getSession()->getUser(); } return $this->getStorage()->log($request, $user); } /** * @see \oat\taoEventLog\model\requestLog\RequestLogStorageReadable::find() * @param array $filters * @param array $options * @return \Iterator * @throws RequestLogException */ public function find(array $filters = [], array $options = []) { if (!$this->getStorage() instanceof RequestLogStorageReadable) { throw new RequestLogException('Request log storage is not readable'); } return $this->getStorage()->find($filters, $options); } /** * @see \oat\taoEventLog\model\requestLog\RequestLogStorageReadable::count() * @param array $filters * @param array $options * @return integer * @throws RequestLogException */ public function count(array $filters = [], array $options = []) { if (!$this->getStorage() instanceof RequestLogStorageReadable) { throw new RequestLogException('Request log storage is not readable'); } return $this->getStorage()->count($filters, $options); } /** * @return RequestLogStorageReadable|RequestLogStorageWritable * @throws */ protected function getStorage() { if ($this->storage === null) { $storageClass = $this->getOption(self::OPTION_STORAGE); if (!class_exists($storageClass)) { throw new RequestLogException('Storage class does not exist'); } $storageParams = $this->getOption(self::OPTION_STORAGE_PARAMETERS)?:[]; $this->storage = new $storageClass($storageParams); $this->getServiceManager()->propagate($this->storage); } return $this->storage; } /** * @param Event $event * @throws */ public function catchEvent(Event $event) { if ($event instanceof BeforeAction && $this->fulfilled) { return; } $this->fulfilled = true; $this->log(); } }