コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function execute(AMQPMessage $msg)
 {
     $msgBody = json_decode($msg->body, true);
     // Log as error to also print it in console when running consumer
     $this->logger->error('RabbitMQ message_logger :: ' . $msgBody['messageData']);
     return true;
 }
コード例 #2
0
ファイル: JobCommand.php プロジェクト: keboola/syrup
 protected function init($jobId)
 {
     $this->jobMapper = $this->getContainer()->get('syrup.elasticsearch.current_component_job_mapper');
     // Get job from ES
     $this->job = $this->jobMapper->get($jobId);
     if ($this->job == null) {
         $this->logger->error("Job id '" . $jobId . "' not found.");
         return self::STATUS_ERROR;
     }
     // SAPI init
     /** @var ObjectEncryptor $encryptor */
     $encryptor = $this->getContainer()->get('syrup.object_encryptor');
     $this->sapiClient = new SapiClient(['token' => $encryptor->decrypt($this->job->getToken()['token']), 'url' => $this->getContainer()->getParameter('storage_api.url'), 'userAgent' => $this->job->getComponent()]);
     $this->sapiClient->setRunId($this->job->getRunId());
     /** @var \Keboola\Syrup\Service\StorageApi\StorageApiService $storageApiService */
     $storageApiService = $this->getContainer()->get('syrup.storage_api');
     $storageApiService->setClient($this->sapiClient);
     $this->storageApiService = $storageApiService;
     /** @var \Keboola\Syrup\Monolog\Processor\JobProcessor $logProcessor */
     $logProcessor = $this->getContainer()->get('syrup.monolog.job_processor');
     $logProcessor->setJob($this->job);
     /** @var \Keboola\Syrup\Monolog\Processor\SyslogProcessor $logProcessor */
     $logProcessor = $this->getContainer()->get('syrup.monolog.syslog_processor');
     $logProcessor->setRunId($this->job->getRunId());
     $logProcessor->setTokenData($storageApiService->getTokenData());
     // Lock DB
     /** @var Connection $conn */
     $conn = $this->getContainer()->get('doctrine.dbal.lock_connection');
     $conn->exec('SET wait_timeout = 31536000;');
     $this->lock = new Lock($conn, $this->job->getLockName());
 }
コード例 #3
0
 /**
  * @param MvcEvent $e
  */
 public function onDispatchError(MvcEvent $e)
 {
     $exception = $e->getParam('exception');
     if (!$exception instanceof \Exception) {
         return;
     }
     $this->logger->error($exception->getTraceAsString());
 }
コード例 #4
0
ファイル: Withdrawal.php プロジェクト: poiuty/midas
 public function doTask($console = false)
 {
     if ($console) {
         $this->info('Processing withdrawals');
     }
     $txn_log = new Logger('TXN');
     $txn_log->pushHandler(new StreamHandler(storage_path() . '/logs/withdrawals_cron.log', Logger::INFO));
     $bar = null;
     if ($console) {
         $total_transactions = WalletsWithdrawal::whereNull('wallets_transaction_id')->where(function (Builder $query) {
             $query->where('need_approve', 0)->orWhere('approved', 1);
         })->count();
         $bar = $this->output->createProgressBar($total_transactions);
     }
     WalletsWithdrawal::whereNull('wallets_transaction_id')->where(function (Builder $query) {
         $query->where('need_approve', 0)->orWhere('approved', 1);
     })->chunk(1000, function ($withdrawals) use($bar, $console, $txn_log) {
         foreach ($withdrawals as $withdrawal) {
             $balance = $withdrawal->wallet->command('getbalance');
             if (empty($balance->error)) {
                 if ($balance * 0.2 >= $withdrawal->amount || $withdrawal->approved == 1) {
                     $amount = $withdrawal->amount - $withdrawal->wallet->transaction_fee;
                     $txn = $withdrawal->wallet->command('sendtoaddress', [$withdrawal->address, $amount]);
                     if (empty($txn->error)) {
                         $wallet_transaction = new WalletsTransaction();
                         $wallet_transaction->txid = $txn;
                         $wallet_transaction->user_id = $withdrawal->user_id;
                         $wallet_transaction->wallet_id = $withdrawal->wallet_id;
                         $wallet_transaction->amount = -$withdrawal->amount;
                         $wallet_transaction->confirmed = false;
                         $wallet_transaction->save();
                         $withdrawal->wallets_transaction_id = $wallet_transaction->id;
                         $withdrawal->save();
                         $txn_log->error('Txn success (wal#' . $withdrawal->wallet->short . ')', (array) $txn);
                     } else {
                         $txn_log->error('Txn error (wal#' . $withdrawal->wallet->short . ')', (array) $txn);
                     }
                 } else {
                     $withdrawal->need_approve = true;
                     $withdrawal->save();
                     $txn_log->error('Too big txn need approve (wal#' . $withdrawal->wallet->short . ') amount:' . $withdrawal->amount, (array) $balance);
                     Cache::flush('need_approve_count');
                 }
             } else {
                 $txn_log->error('Getting balance(wal#' . $withdrawal->wallet->short . ') error', (array) $balance);
             }
             if ($console) {
                 $bar->advance();
             }
         }
     });
     if ($console) {
         $bar->finish();
         echo PHP_EOL;
         $this->info('done');
     }
 }
コード例 #5
0
 public function onError(ErrorEvent $event)
 {
     if ($event->getResponse()) {
         $this->log->error('guzzle_error: ' . $event->getException()->getMessage());
     } else {
         $ex = $event->getException();
         $this->log->error($ex->getMessage() . ' -- ' . $ex->getTraceAsString(), [$ex->getCode(), $ex->getLine(), $ex->getFile()]);
     }
     $event->stopPropagation();
 }
コード例 #6
0
ファイル: Setup.php プロジェクト: kriby/test_framework
 public function execute()
 {
     try {
         $this->install->install();
     } catch (\Exception $e) {
         $this->logger->error($e->getTraceAsString());
         echo $e->getMessage();
     }
     $this->response->redirect('/');
 }
コード例 #7
0
 public function __construct($orchestrate_api_key, Logger $logger)
 {
     try {
         $this->orchestrate = new Client($orchestrate_api_key);
         $this->logger = $logger;
     } catch (Exception $e) {
         $logger->error($e->getMessage());
     } catch (ClientException $e) {
         $logger->error($e->getMessage());
     }
 }
コード例 #8
0
 public function buildForPreprocessor(Request $request, Route $preprocessorRoute)
 {
     // Localhost case
     if ($preprocessorRoute->getDefault('_http_host') === $request->getHttpHost()) {
         $this->logger->info("InternalForwarder built for preprocessor: Localhost forwarder.", ['host' => $request->getHttpHost()]);
         return $this->container->get('prestashop.public_writer.protocol.internal_forwarder.localhost');
     }
     // Error case: localhost case was not matching, but there is no other forwarder available.
     $this->logger->error("InternalForwarder built for preprocessor: NO forwarder found to reach distant host.", ['host' => $request->getHttpHost()]);
     throw new \ErrorException("InternalForwarder building for preprocessor: NO forwarder found to reach distant host: " . $request->getHttpHost());
 }
コード例 #9
0
ファイル: MongoProvider.php プロジェクト: pjeroo/test_ngs
 public function safeBatchInsert($db, $collection, array $data, array $options = [])
 {
     try {
         return $this->mongo->{$db}->{$collection}->batchInsert($data, $options);
     } catch (\MongoException $e) {
         if ($this->monolog) {
             $this->monolog->error('Mongo error', ['class' => __CLASS__, 'message' => $e->getMessage()]);
         }
         return false;
     }
 }
コード例 #10
0
 /**
  * @param \Exception $e
  * @param $code
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  */
 public function handle(\Exception $e, $code)
 {
     switch ($code) {
         case 404:
             $data = array('success' => false, 'code' => '404', 'message' => 'The requested page could not be found.');
             break;
         default:
             $data = array('success' => false, 'code' => $code, 'message' => $e->getMessage());
     }
     $this->logger->error(sprintf('%s: %s', get_class($e), $e->getMessage()));
     return new JsonResponse($data, $code);
 }
コード例 #11
0
ファイル: Client.php プロジェクト: luizfk/pipedrive-test-task
 /**
  * @param       $method
  * @param       $endPoint
  * @param array $formParams
  *
  * @throws \Exception
  *
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function request($method, $endPoint, array $formParams = [])
 {
     $options = ['headers' => ['Accept' => 'application/json'], 'form_params' => $formParams];
     $requestUrl = $this->formatRequestUrl($endPoint);
     $this->logger->info('Remote request: ', ['method' => $method, 'url' => $requestUrl, 'options' => $options]);
     try {
         $response = $this->httpClient->{$method}($requestUrl, $options);
     } catch (\Exception $e) {
         $this->logger->error('Api request error.', ['exception' => $e->getMessage()]);
         throw $e;
     }
     $this->logger->info('Remote response: ', ['statusCode' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'body' => $response->getBody()]);
     return $response;
 }
コード例 #12
0
ファイル: Client.php プロジェクト: zarincheg/celium
 /**
  * Sending request to Celium Node for run action
  * @param string $request
  * @throws \Exception
  * @return string Key of request. That's need for find results.
  */
 public function sendRequest($request)
 {
     $requestKey = md5($request);
     $request = json_decode($request, true);
     $request['key'] = $requestKey;
     $request = json_encode($request);
     $b = $this->rabbit->write($request, $this->name . '_request');
     if (!$b) {
         $this->logger->error('Request to the node failed', ['nodeName' => $this->name, 'requestBody' => $request, 'requestKey' => $requestKey, 'tags' => ['client-request-failed']]);
         throw new \Exception("Failed to add the request to the queue");
     }
     $this->logger->info('Request was sent to the node', ['nodeName' => $this->name, 'requestBody' => $request, 'requestKey' => $requestKey, 'tags' => ['client-request']]);
     return $requestKey;
 }
コード例 #13
0
ファイル: ChartsBuilder.php プロジェクト: ribrain80/suncharts
 /**
  * Clears Up the images folder
  * Old files are unlinked
  */
 public function clearPast()
 {
     // # DirecotryIterator on image folder
     $chartsIterator = new \DirectoryIterator($this->PM->getProperty("basePath") . "/imagedata/");
     $isDirEmpty = !$chartsIterator->valid();
     // # Check if it is already empty
     if (!$isDirEmpty) {
         $this->log->error("Unlinking old files i've found");
         // # Unlink 'em all
         foreach ($chartsIterator as $file) {
             if ($file->isFile()) {
                 @unlink($this->PM->getProperty("basePath") . "/imagedata/" . $file->getFilename());
             }
         }
     }
 }
コード例 #14
0
ファイル: ConfigListener.php プロジェクト: visapi/amun
 public function notify(Service\Record $record, DOMDocument $config, Logger $logger)
 {
     $permissions = $config->getElementsByTagName('permissions')->item(0);
     if ($permissions !== null) {
         $logger->info('Create user permissions');
         try {
             $namespace = str_replace('\\', '_', strtolower($record->namespace));
             $pos = strpos($namespace, '_');
             $namespace = substr($namespace, $pos !== false ? $pos + 1 : 0);
             // remove vendor part
             $perms = $permissions->childNodes;
             for ($i = 0; $i < $perms->length; $i++) {
                 $perm = $perms->item($i);
                 if (!$perm instanceof DOMElement) {
                     continue;
                 }
                 if ($perm->nodeName == 'perm') {
                     $name = $perm->getAttribute('name');
                     $desc = $perm->getAttribute('description');
                     if (!empty($name) && !empty($desc)) {
                         $name = $namespace . '_' . $name;
                         $this->sql->insert($this->registry['table.user_right'], array('serviceId' => $record->id, 'name' => $name, 'description' => $desc));
                         $logger->info('> Created permission "' . $name . '"');
                     }
                 }
             }
         } catch (\Exception $e) {
             $logger->error($e->getMessage());
         }
     }
 }
コード例 #15
0
ファイル: BaseAPI.php プロジェクト: kaigan/qbankapi2wrapper
 protected function callAsync($function, array $data, $log = false)
 {
     if (strtolower($function) == 'login') {
         $this->wrapperLog->error('Login can not be called asynchronously!');
         throw new APIException('Login can not be called asynchronously!');
     }
     if (!empty($this->hash)) {
         $data['hash'] = $this->hash;
     }
     $socket = fsockopen(parse_url($this->apiAddress, PHP_URL_HOST), 80, $errno, $errstr);
     if ($socket === false) {
         $this->wrapperLog->critical('Error while opening asynchronous socket: ' . $errstr, array('code' => $errno));
         throw new ConnectionException('Error while opening asynchronous socket: ' . $errstr, $errno);
     }
     $data = 'data=' . urlencode(json_encode($data));
     $msg = 'POST /' . $this->qbankAddress . '/' . $function . ' HTTP/1.1' . "\r\n";
     $msg .= 'Host:' . parse_url($this->apiAddress, PHP_URL_HOST) . "\r\n";
     $msg .= 'Content-type: application/x-www-form-urlencoded' . "\r\n";
     $msg .= 'Content-length: ' . strlen($data) . "\r\n";
     $msg .= 'Connection: Close' . "\r\n\r\n";
     $msg .= $data;
     $result = fwrite($socket, $msg);
     if ($result === false) {
         $this->wrapperLog->critical('Error while writing to asycnhronous socket!');
         throw new ConnectionException('Error while writing to asycnhronous socket!');
     }
     @fclose($socket);
 }
コード例 #16
0
ファイル: MapperConnection.php プロジェクト: wwtg99/data_pool
 /**
  * @param string $name
  * @return IDataMapper
  */
 public function getMapper($name)
 {
     if (array_key_exists($name, $this->mappers)) {
         $m = $this->mappers[$name];
         if ($m instanceof IDataMapper) {
             return $m;
         }
     } else {
         try {
             if ($this->prepend_mapper_path && $this->mapper_path) {
                 $name = $this->mapper_path . '\\' . $name;
             }
             $rc = new \ReflectionClass($name);
             if ($rc->implementsInterface('\\Wwtg99\\DataPool\\Common\\IDataMapper')) {
                 $ins = $rc->newInstance();
                 if ($ins instanceof IDataMapper) {
                     $ins->setEnvironment($this);
                     $this->mappers[$name] = $ins;
                     return $ins;
                 }
             }
         } catch (\ReflectionException $e) {
             $msg = Message::messageList(4);
             $this->logger->error($msg->getMsg() . '  ' . $e->getMessage());
         }
     }
     return null;
 }
コード例 #17
0
ファイル: ConfigListener.php プロジェクト: visapi/amun
 public function notify(Service\Record $record, DOMDocument $config, Logger $logger)
 {
     $api = $config->getElementsByTagName('api')->item(0);
     if ($api !== null) {
         $logger->info('Create api');
         try {
             $services = $api->childNodes;
             for ($i = 0; $i < $services->length; $i++) {
                 $service = $services->item($i);
                 if (!$service instanceof DOMElement) {
                     continue;
                 }
                 if ($service->nodeName == 'service') {
                     $types = $service->getElementsByTagName('type');
                     $uri = $service->getElementsByTagName('uri')->item(0);
                     if ($uri instanceof DOMElement) {
                         $endpoint = rtrim($record->path . $uri->nodeValue, '/');
                         $this->sql->insert($this->registry['table.xrds'], array('serviceId' => $record->id, 'priority' => 0, 'endpoint' => $endpoint));
                         $apiId = $this->sql->getLastInsertId();
                         foreach ($types as $type) {
                             $this->sql->insert($this->registry['table.xrds_type'], array('apiId' => $apiId, 'type' => $type->nodeValue));
                         }
                         $logger->info('> Register endpoint ' . $endpoint);
                     }
                 }
             }
         } catch (\Exception $e) {
             $logger->error($e->getMessage());
         }
     }
 }
コード例 #18
0
ファイル: Logger.php プロジェクト: acgrid/adbot
 public function error($message, array $args = [], array $context = [])
 {
     if (count($args)) {
         $message = vsprintf($message, $args);
     }
     return parent::error($message, $context);
 }
コード例 #19
0
 /**
  * Handles incoming webhook requests.
  */
 public function index()
 {
     // set up logging to email
     $this->log = Log::getMonolog();
     if ($to = config('auto-deploy.notify')) {
         $domain = parse_url(config('app.url'), PHP_URL_HOST);
         $msg = \Swift_Message::newInstance('Project Deployed')->setFrom(["do_not_reply@{$domain}" => "Laravel Auto-Deploy[{$domain}]"])->setTo($to)->setBody('', 'text/html');
         $handler = new SwiftMailerHandler(Mail::getSwiftMailer(), $msg, Logger::NOTICE);
         $handler->setFormatter(new HtmlFormatter());
         $this->log->pushHandler($handler);
     }
     // check to see if we should execute this event
     if (in_array($this->origin->event(), array_keys(config("auto-deploy.{$this->origin->name}")))) {
         // get the parameters for the event we're handling
         $configKey = "auto-deploy.{$this->origin->name}.{$this->origin->event()}";
         $this->webroot = config("{$configKey}.webroot");
         $this->installDir = dirname($this->webroot) . '/' . date('Y-m-d') . '_' . $this->commitId;
         $steps = config("{$configKey}.steps");
         // execute the configured steps
         $this->result = ['Commit_ID' => $this->commitId, 'Timestamp' => date('r'), 'output' => ''];
         $whitelist = ['backupDatabase', 'pull', 'copyEnv', 'composer', 'npm', 'migrate', 'seed', 'deploy'];
         foreach ($steps as $step) {
             if (in_array($step, $whitelist) && !$this->{$step}()) {
                 $this->log->error('Deploy failed.', $this->result);
                 return;
             }
         }
         $this->log->notice('Deploy succeeded!', $this->result);
     } else {
         $this->log->error('Deploy failed.', ['Reason' => 'This event was not configured.']);
     }
 }
コード例 #20
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     ini_set('memory_limit', '512M');
     $files = $input->getArgument('file');
     if (!$files || count($files) === 0) {
         $fp = $this->getContainer()->get('filepaths');
         $files[] = $fp->getOnixPath('xml');
         $files[] = $fp->getOnixPath('csv');
     }
     foreach ($files as $file) {
         $this->logger->info("Writing {$file}");
         $ext = pathinfo($file, PATHINFO_EXTENSION);
         switch ($ext) {
             case 'xml':
                 $this->generateXml($file);
                 break;
             case 'csv':
                 $this->generateCsv($file);
                 break;
             default:
                 $this->logger->error("Cannot generate {$ext} ONIX format.");
                 break;
         }
     }
 }
コード例 #21
0
ファイル: Service.php プロジェクト: itmh/service-tools
 /**
  * Вызывает соответствующий метод в конкретной реализации,
  * самостоятельно занимаясь кэшированием, логгированием
  * и измерением времени
  *
  * @param string $method Вызываемый метод
  * @param array  $args   Переданные в метод аргументы
  *
  * @return Response
  * @throws ConfigurationErrorException
  * @throws \InvalidArgumentException
  */
 public function __call($method, array $args = [])
 {
     $cacheKey = md5(serialize(func_get_args()));
     $args = $this->createArgs($args);
     $timer = $this->pinba->start(['call' => sprintf('%s->%s', get_class($this), $method), 'args' => $args]);
     $this->logger->info(sprintf('%s->%s', get_class($this), $method), $args);
     $this->checkIsConfigured();
     $item = $this->cacher->getItem($cacheKey);
     $inCache = !$item->isMiss();
     $this->logger->info(sprintf('item found in cache: %s', $inCache ? 'yes' : 'no'));
     if ($inCache) {
         $this->logger->info('get from cache');
         $response = $item->get();
     } else {
         $this->logger->info('get from source');
         $response = $this->implementation($method, $args);
         $expiresDefault = array_key_exists('default', $this->cacherExpires) ? $this->cacherExpires['default'] : 0;
         $expires = array_key_exists($method, $this->cacherExpires) ? $this->cacherExpires[$method] : $expiresDefault;
         $item->set($response, $expires);
         if (!$response->isOk()) {
             $this->logger->error('error response', [$response->getError()]);
         } else {
             $this->logger->debug('successful response', [$response->getContent()]);
         }
     }
     $this->pinba->stop($timer);
     $this->logger->info('pinba', $this->pinba->info($timer));
     return $response;
 }
コード例 #22
0
ファイル: ConfigListener.php プロジェクト: visapi/amun
 public function notify(Service\Record $record, DOMDocument $config, Logger $logger)
 {
     $activity = $config->getElementsByTagName('activity')->item(0);
     if ($activity !== null) {
         $logger->info('Create user activity template');
         try {
             $templates = $activity->childNodes;
             for ($i = 0; $i < $templates->length; $i++) {
                 $template = $templates->item($i);
                 if (!$template instanceof DOMElement) {
                     continue;
                 }
                 if ($template->nodeName == 'template') {
                     $type = $template->getAttribute('type');
                     $verb = $template->getAttribute('verb');
                     $table = $template->getAttribute('table');
                     $path = $template->getAttribute('path');
                     $summary = $template->nodeValue;
                     if (isset($this->registry['table.' . $table])) {
                         $table = $this->registry['table.' . $table];
                     } else {
                         throw new Exception('Invalid table ' . $table);
                     }
                     if (!empty($type) && !empty($verb) && !empty($table) && !empty($summary)) {
                         $this->sql->insert($this->registry['table.user_activity_template'], array('type' => $type, 'verb' => $verb, 'table' => $table, 'path' => $path, 'summary' => $summary));
                         $logger->info('> Created user activity template');
                         $logger->info($summary);
                     }
                 }
             }
         } catch (\Exception $e) {
             $logger->error($e->getMessage());
         }
     }
 }
コード例 #23
0
ファイル: Hook.php プロジェクト: kdambekalns/apigenerator.org
 /**
  * @param \Exception $exception
  */
 public function handleException($exception)
 {
     $message = '';
     $e = $exception;
     do {
         $message .= sprintf("%s:%d: [%s] (%d) %s\n", $e->getFile(), $e->getLine(), get_class($e), $e->getCode(), $e->getMessage());
         $e = $e->getPrevious();
     } while ($e);
     $this->logger->error(rtrim($message));
     while (count(ob_list_handlers())) {
         ob_end_clean();
     }
     header("HTTP/1.1 500 Internal Server Error");
     echo '500 Internal Server Error';
     exit(1);
 }
コード例 #24
0
 protected function stopDaemon()
 {
     $pid = $this->getRunningPid();
     if (!$pid) {
         $this->logger->notice("Cannot stop the daemon as it is not started");
         return "notstarted";
     }
     $this->logger->info("Trying to terminate the daemon", array('pid' => $pid));
     $this->processControl->kill($pid);
     for ($i = 0; ($pid = $this->getRunningPid()) && $i < 5; $i++) {
         sleep(1);
     }
     if ($pid) {
         $this->logger->warning("The daemon is resiting, trying to kill it", array('pid' => $pid));
         $this->processControl->kill($pid, true);
         for ($i = 0; ($pid = $this->getRunningPid()) && $i < 5; $i++) {
             sleep(1);
         }
     }
     if (!$pid) {
         $this->logger->notice("Daemon stopped");
         return "stopped";
     }
     $this->logger->error("Could not stop the daemon");
 }
コード例 #25
0
ファイル: Inspector.php プロジェクト: jamset/tasks-inspector
 protected function makeAttentionLog($inspectionMessage)
 {
     if (!$this->allTasksComplete) {
         $this->logger->error("Inspection message: " . $inspectionMessage);
     }
     $this->logger->warning("Executed with Error: " . serialize($this->executedWithError));
     return null;
 }
コード例 #26
0
ファイル: Logger.php プロジェクト: duchkinanast/composer
 public static function sayHi()
 {
     $log = new newClass('name');
     $log->pushHandler(new StreamHandler(__DIR__ . '/new.log', newClass::WARNING));
     // add records to the log
     $log->warning('Foo');
     $log->error('Bar');
 }
コード例 #27
0
ファイル: DeployManager.php プロジェクト: andou/gancin
 protected function log($message, $error = FALSE)
 {
     if ($error) {
         $this->logger->error($message);
     } else {
         $this->logger->info($message);
     }
 }
コード例 #28
0
ファイル: Worker.php プロジェクト: metro-q/metro
 private function logException(\Exception $e, $jobId)
 {
     $trace = isset($e->traceString) ? $e->traceString : $e->getTraceAsString();
     $this->logger->error("Job {$jobId} failed: " . $e->getMessage(), ['exception' => $e]);
     foreach (explode(PHP_EOL, $trace) as $line) {
         $this->logger->error($line);
     }
 }
コード例 #29
0
 public function onOAuthServerTokenIssued(OAuthServerEvent $event)
 {
     if ($device_token = $event->getRequest()->get('device_token')) {
         $deviceOS = null;
         if ($this->pushDeviceService->detect->match("iOS")) {
             $deviceOS = "iOS";
         } elseif ($this->pushDeviceService->detect->match("AndroidOS")) {
             $deviceOS = "AndroidOS";
         }
         if (null === $deviceOS) {
             $this->logger->error('Cannot detect device OS', ['PushNotification']);
             return;
         }
         $response = json_decode($event->getResponse()->getContent(), true);
         $token = $this->accessTokenManager->findTokenByToken($response['access_token']);
         $this->pushDeviceService->upsert($device_token, $deviceOS, $token->getUser()->getId());
     }
 }
コード例 #30
0
 /**
  * @param $message
  * @param $recipients
  * @return bool|int   if TRUE - all messages are sent, otherwise number of not delivered messages
  */
 public function push($text, $recipients, array $extra = null, $badge = 0)
 {
     $this->openStream();
     if (is_bool($recipients) || is_array($recipients) && count($recipients) == 0) {
         $this->logger->error('Recipient is not a string, integer or array. Or empty array. Message will not be delivered', [$this->loggerContext]);
         return false;
     }
     if (!is_array($recipients)) {
         $recipients = [$recipients];
     }
     foreach ($recipients as $recipient) {
         $deviceToken = $recipient->getDeviceToken();
         $message = new ApnsMessage($deviceToken);
         $message->setText($text);
         //            $message->setCustomIdentifier(sprintf("Message-Badge-%03d", $i));
         $message->setBadge($badge);
         $message->setSound();
         if (is_array($extra) && count($extra) > 0) {
             foreach ($extra as $key => $value) {
                 $message->setCustomProperty($key, $value);
             }
         }
         $this->eventDispatcher->dispatch(PushMessageEvents::AFTER_CREATED, new PushMessageEvent($message, $deviceToken));
         $this->service->add($message);
     }
     $this->service->send();
     $this->closeStream();
     $aErrorQueue = $this->service->getErrors();
     if (!empty($aErrorQueue)) {
         $this->logger->error(var_export($aErrorQueue, true));
         foreach ($aErrorQueue as $errorQuery) {
             foreach ($errorQuery['ERRORS'] as $error) {
                 if ($error['statusCode'] == 8) {
                     // token not found
                     $invalidDeviceToken = $errorQuery['MESSAGE']->getRecipient();
                     $this->deviceService->remove($invalidDeviceToken);
                     $this->logger->info('Removing invalid token #' . $invalidDeviceToken);
                 }
             }
         }
         return count($aErrorQueue);
     }
     return true;
 }