/** * Actually do the logging. * * @param EventInterface $event */ public function log(EventInterface $event) { $params = $event->getParams(); $method = '?'; $exception = isset($params['exception']) ? $params['exception'] : ''; $sql = isset($params['sql']) ? $params['sql'] : array(); $this->logger->critical(get_class($event->getTarget()) . "::{$method} - {$exception}", $sql); }
/** * {@inheritdoc} */ public function getController(Request $request) { if (!($controller = $request->attributes->get('_controller'))) { if ($this->logger !== NULL) { $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing'); } return FALSE; } return $this->getControllerFromDefinition($controller, $request->getPathInfo()); }
private function logAndDispatchEvent($name, Lock $lock, $lockName) { if ($this->logger) { $context = array('service' => $this->originalServiceClass, 'method' => $lock->getMethod(), 'lock' => $lockName); $this->logger->log(\Psr\Log\LogLevel::INFO, $name, $context); } if ($this->eventDispatcher) { $this->eventDispatcher->dispatch($name, new LockEvent($lock, $this->originalService)); } }
/** * Return an error into an HTTP or JSON data array. * * @param string $title * @return array */ public function error($title = null) { if ($title == null) { $title = $this->debug ? 'The application could not run because of the following error:' : 'A website error has occurred. Sorry for the temporary inconvenience.'; } $type = $this->request->getHeader('Content-Type'); $mesg = $this->exception->getMessage(); $file = $this->exception->getFile(); $line = $this->exception->getLine(); $code = $this->exception->getCode(); $statusCode = method_exists($this->exception, 'getStatusCode') ? $this->exception->getStatusCode() : null; // Check status code is null if ($statusCode == null) { $statusCode = $code >= 100 && $code <= 500 ? $code : 400; } $this->response->withStatus($statusCode); // Check logger exist if ($this->logger !== null) { // Send error to log $this->logger->addError($this->exception->getMessage()); } $this->isJson = isset($type[0]) && $type[0] == 'application/json'; // Check content-type is application/json if ($this->isJson) { // Define content-type to json $this->response->withHeader('Content-Type', 'application/json'); $error = ['status' => 'error', 'status_code' => $statusCode, 'error' => $title, 'details' => []]; // Check debug if ($this->debug) { $error['details'] = ['message' => $mesg, 'file' => $file, 'line' => $line, 'code' => $code]; } return $error; } // Define content-type to html $this->response->withHeader('Content-Type', 'text/html'); $message = sprintf('<span>%s</span>', htmlentities($mesg)); $error = ['type' => get_class($this->exception), $error['status_code'] = $statusCode, 'message' => $message]; // Check debug if ($this->debug) { $trace = $this->exception->getTraceAsString(); $trace = sprintf('<pre>%s</pre>', htmlentities($trace)); $error['file'] = $file; $error['line'] = $line; $error['code'] = $code; $error['trace'] = $trace; } $error['debug'] = $this->debug; $error['title'] = $title; return $error; }
/** * Send a message to Queue about indexing entry. * * @param EventInterface $event */ public function index(EventInterface $event) { try { $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('search-index', false, false, false, false); $params = $event->getParams(); $msg = new AMQPMessage(json_encode($params)); $channel->basic_publish($msg, '', 'search-index'); $channel->close(); $connection->close(); } catch (\Exception $e) { $this->logger->warn("Queue Service when indexing entries: " . $e->getMessage()); } }
/** * @return $this * @throws NotifyException */ public function send() { //DATA-OBJECTS // get data-objects from persistence layer. $eventObject = $this->getEvent($this->params->event_id); $userObject = $this->getUser($this->params->recipients); //VIEW // create and configure view $child = new ViewModel(array('user' => $userObject, 'event' => $eventObject)); $child->setTemplate($this->params->type ? 'attend' : 'unattend'); $layout = new ViewModel(); $layout->setTemplate('layout'); $layout->addChild($child, 'content'); $phpRenderer = new PhpRenderer(); $phpRenderer->setCanRenderTrees(true); $resolver = new Resolver\TemplateMapResolver(); $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'attend' => __DIR__ . '/../../../view/email/attending.phtml', 'unattend' => __DIR__ . '/../../../view/email/un-attending.phtml')); $phpRenderer->setResolver($resolver); foreach ($layout as $child) { $child->setOption('has_parent', true); $result = $phpRenderer->render($child); $child->setOption('has_parent', null); $capture = $child->captureTo(); if (!empty($capture)) { $layout->setVariable($capture, $result); } } //MESSAGE // create and configure message. $message = new Mail(); $message->body = $phpRenderer->render($layout); $message->email = $userObject->email; $message->name = $userObject->name; $message->subject = $this->params->type ? "Þú hefur skráð þig á viðburðinn: {$eventObject->subject}" : "Þú hefur afskráð þig af viðburðinum: {$eventObject->subject}"; //MAIL // now we want to send this to the user/quest via e-mail // so we try to connect to Queue and send a message // to mail_queue try { $connection = $this->queueFactory->createConnection(); $channel = $connection->channel(); $channel->queue_declare('mail_queue', false, true, false, false); $msg = new AMQPMessage($message->serialize(), ['delivery_mode' => 2]); $this->logger->info("{$userObject->email} is " . ($this->params->type ? '' : 'not ') . "attending {$eventObject->subject}"); $channel->basic_publish($msg, '', 'mail_queue'); } catch (\Exception $e) { throw new NotifyException($e->getMessage(), 0, $e); } finally { if (isset($channel) && $channel) { $channel->close(); } if (isset($connection) && $connection) { $connection->close(); } $eventObject = null; $userObject = null; $this->closeDataSourceDriver(); } return $this; }
private function send($recipients, $subject, $body) { $channel = false; $connection = false; try { $connection = $this->queueFactory->createConnection(); $channel = $connection->channel(); $channel->queue_declare('mail_queue', false, true, false, false); foreach ($recipients as $recipient) { $message = new Mail(); $message->name = $recipient->name; $message->email = $recipient->address; $message->subject = $subject; $message->body = $body; $msg = new AMQPMessage($message->serialize(), ['delivery_mode' => 2]); $channel->basic_publish($msg, '', 'mail_queue'); } } catch (\Exception $e) { while ($e) { $this->logger->critical(get_class($this) . ":send says: {$e->getMessage()}", $e->getTrace()); $e = $e->getPrevious(); } } finally { if (isset($channel) && $channel) { $channel->close(); } if (isset($connection) && $connection) { $connection->close(); } } }
/** * {@inheritDoc} */ public function refreshIndex(IndexInterface $index) { try { $this->client->indices()->refresh(['index' => $index->getName()]); } catch (\Exception $e) { $this->logger->error($e->getMessage()); } return $this; }
/** * @param string $endpoint * @return bool * @throws \Exception */ public function isApiRateLimitReached($endpoint = '/statuses/show/:id') { $rateLimitStatus = $this->fetchRateLimitStatus(); if ($this->hasError($rateLimitStatus)) { $message = $rateLimitStatus->errors[0]->message; $this->logger->error($message); throw new \Exception($message, $rateLimitStatus->errors[0]->code); } else { $leastUpperBound = $rateLimitStatus->resources->statuses->{$endpoint}->limit; $remainingCalls = $rateLimitStatus->resources->statuses->{$endpoint}->remaining; $remainingCallsMessage = $this->translator->transChoice('logs.info.calls_remaining', $remainingCalls, ['{{ count }}' => $remainingCalls, '{{ endpoint }}' => $endpoint, '{{ identifier }}' => substr($this->userToken, 0, '8')], 'logs'); $this->logger->info($remainingCallsMessage); return $remainingCalls < floor($leastUpperBound * (1 / 10)); } }
/** * @return $this * @throws NotifyException */ public function send() { //ID // create an ID for this digest $emailId = $this->getHash(); //TIME RANGE // calculate time range and create from and // to date objects for the range. $from = new DateTime(); $from->add(new DateInterval('P1D')); $to = new DateTime(); $to->add(new DateInterval('P8D')); $this->logger->info("Queue Service says: Fetching upcoming events"); //EVENTS // fetch all events $events = $this->getEvents($from, $to); //NO EVENTS // if there are no events to publish, then it's no need // to keep on processing this script if (count($events) == 0) { $this->logger->info("Digest, no events registered, stop"); return $this; } else { $this->logger->info("Digest, " . count($events) . " events registered."); } //USERS // get all users who want to know // about the upcoming events. $users = $this->getUsers(); $this->logger->info("Digest, " . count($users) . " user will get email "); //VIEW // create and configure view $child = new ViewModel(array('events' => $events, 'from' => $from, 'to' => $to)); $child->setTemplate('news-digest'); $layout = new ViewModel(); $layout->setTemplate('layout'); $layout->addChild($child, 'content'); $phpRenderer = new PhpRenderer(); $phpRenderer->setCanRenderTrees(true); $resolver = new TemplateMapResolver(); $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'news-digest' => __DIR__ . '/../../../view/email/news-digest.phtml')); $phpRenderer->setResolver($resolver); //QUEUE // try to connect to Queue and send messages to it. // this will try to send messages to mail_queue, that will // send them on it's way via a MailTransport try { $connection = $this->queueFactory->createConnection(); $channel = $connection->channel(); $channel->queue_declare('mail_queue', false, true, false, false); foreach ($users as $user) { $child->setVariable('user', $user); foreach ($layout as $child) { $child->setOption('has_parent', true); $result = $phpRenderer->render($child); $child->setOption('has_parent', null); $capture = $child->captureTo(); if (!empty($capture)) { $layout->setVariable($capture, $result); } } $result = new Mail(); $result->name = $user->name; $result->email = $user->email; $result->subject = "Vikan framundan | {$from->format('j. n.')} - {$to->format('j. n. Y')}"; $result->body = $phpRenderer->render($layout); $result->id = $emailId; $result->user_id = md5((string) $emailId . $user->email); $result->type = 'Digest'; $result->parameters = 'allir'; $result->test = false; $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]); $channel->basic_publish($msg, '', 'mail_queue'); $this->logger->debug("Queue Service says: Fetching users who want upcoming events, {$user->email} in queue "); } } catch (\Exception $e) { throw new NotifyException($e->getMessage(), 0, $e); } finally { if (isset($channel) && $channel) { $channel->close(); } if (isset($connection) && $connection) { $connection->close(); } $this->closeDataSourceDriver(); } return $this; }
/** * Actually do the logging. * * @param EventInterface $event */ public function log(EventInterface $event) { $params = $event->getParams(); $method = isset($params[0]) ? $params[0] : ''; $this->logger->debug(get_class($event->getTarget()) . "::{$method} - {$event->getName()}"); }
/** * Run the handler. * * @return $this * @throws NotifyException */ public function send() { $emailId = $this->getHash(); $users = $this->getUsers($this->params->recipients, $this->params->test, $this->params->sender_id, $this->params->group_id); $group = $this->getGroup($this->params->group_id); $this->logger->info("Group-email in " . ($this->params->test ? '' : 'none') . " test mode"); //MAIL // now we want to send this to the user/quest via e-mail // so we try to connect to Queue and send a message // to mail_queue try { //QUEUE // create and configure queue $connection = $this->queueFactory->createConnection(); $channel = $connection->channel(); $channel->queue_declare('mail_queue', false, true, false, false); //VIEW // create and configure view $child = new ViewModel(array('user' => null, 'group' => $group, 'body' => call_user_func(new Paragrapher(), $this->params->body))); $child->setTemplate('script'); $layout = new ViewModel(); $layout->setTemplate('layout'); $layout->addChild($child, 'content'); $phpRenderer = new PhpRenderer(); $phpRenderer->setCanRenderTrees(true); $resolver = new Resolver\TemplateMapResolver(); $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'script' => __DIR__ . '/../../../view/email/group-letter.phtml')); $phpRenderer->setResolver($resolver); //FOR EVERY USER // for every user, render mail-template // and send to mail-queue foreach ($users as $user) { $child->setVariable('user', $user); foreach ($layout as $child) { $child->setOption('has_parent', true); $result = $phpRenderer->render($child); $child->setOption('has_parent', null); $capture = $child->captureTo(); if (!empty($capture)) { $layout->setVariable($capture, $result); } } $result = new Mail(); $result->name = $user->name; $result->email = $user->email; $result->subject = $this->params->subject; $result->body = $phpRenderer->render($layout); $result->user_id = md5((string) $emailId . $user->email); $result->id = $emailId; $result->type = 'Event'; $result->entity_id = $group->id; $result->parameters = $this->params->recipients; $result->test = $this->params->test; $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]); $this->logger->info("Groupmail to user:{$user->email}, group:{$group->name_short}"); $channel->basic_publish($msg, '', 'mail_queue'); } } catch (\Exception $e) { throw new NotifyException($e->getMessage(), 0, $e); } finally { if (isset($channel) && $channel) { $channel->close(); } if (isset($connection) && $connection) { $connection->close(); } $users = null; $group = null; $this->closeDataSourceDriver(); } return $this; }
/** * @return $this * @throws NotifyException */ public function send() { $emailId = $this->getEmailId(); $event = $this->getEvent($this->params->event_id); $users = $this->getUser($this->getGroupsFromEvent($event), $event->id, $this->params->user_id, $this->params->recipients, $this->params->test); $this->logger->info(count($users) . " user will get an email" . "in connection with event {$event->subject}:{$event->id}"); //VIEW // create and configure view $child = new ViewModel(array('user' => null, 'event' => $event, 'body' => call_user_func(new Paragrapher(), $this->params->body))); $child->setTemplate('event'); $layout = new ViewModel(); $layout->setTemplate('layout'); $layout->addChild($child, 'content'); $phpRenderer = new PhpRenderer(); $phpRenderer->setCanRenderTrees(true); $resolver = new Resolver\TemplateMapResolver(); $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'event' => __DIR__ . '/../../../view/email/event.phtml')); $phpRenderer->setResolver($resolver); //CONNECT TO QUEUE // try to connect to RabbitMQ try { $connection = $this->queueFactory->createConnection(); $channel = $connection->channel(); $channel->queue_declare('mail_queue', false, true, false, false); //FOR EVER USER // for every user: render email template, create message object and // send to mail-queue foreach ($users as $user) { $child->setVariable('user', $user); foreach ($layout as $child) { $child->setOption('has_parent', true); $result = $phpRenderer->render($child); $child->setOption('has_parent', null); $capture = $child->captureTo(); if (!empty($capture)) { $layout->setVariable($capture, $result); } } $message = new Mail(); $message->name = $user->name; $message->email = $user->email; $message->subject = $this->params->subject; $message->body = $phpRenderer->render($layout); $message->id = $emailId; $message->user_id = md5((string) $emailId . $user->email); $message->type = 'Event'; $message->entity_id = $event->id; $message->parameters = $this->params->recipients; $message->test = $this->params->test; $msg = new AMQPMessage($message->serialize(), ['delivery_mode' => 2]); $this->logger->info(" {$user->email} will get an email" . "in connection with event {$event->subject}:{$event->id}"); $channel->basic_publish($msg, '', 'mail_queue'); } } catch (\Exception $e) { throw new NotifyException($e->getMessage(), 0, $e); } finally { if (isset($channel) && $channel) { $channel->close(); } if (isset($connection) && $connection) { $connection->close(); } $users = null; $event = null; $this->closeDataSourceDriver(); } return $this; }