private function configure($controllersNamespace, $defaultController, $defaultAction, $requestAction) { // checking base controller namespace if (!$controllersNamespace) { $this->logger->critical('Action: interface namespace is not configured'); throw new ApiServerException(sprintf('Action of interface "%s" error: controllers namespace not configured', $this->getInterfaceName())); } // reset variables $path = []; $controller = null; $action = null; // parsing request action $requestAction = str_replace(['/', '\\'], ':', $requestAction); $requestAction = trim($requestAction, ':'); if ($requestAction) { $requestActionParts = explode(':', $requestAction); if (count($requestActionParts) < 2) { $controller = ucfirst($requestActionParts[0]); } else { $action = lcfirst(array_pop($requestActionParts)); $controller = ucfirst(array_pop($requestActionParts)); $path = $requestActionParts; } } if (!$controller) { $controller = ucfirst($defaultController); } if (!$action) { $action = lcfirst($defaultAction); } if (!$controller || !$action) { $this->logger->notice(sprintf('Action: unable to route action "%s"', $requestAction)); throw new ApiServerException(sprintf('Action of interface "%s" error: unable to route action "%s"', $this->getInterfaceName(), $requestAction)); } foreach ($path as &$subPath) { $subPath = ucfirst($subPath); } // calculating local class name $localClassName = $path; array_unshift($localClassName, 'Request'); array_push($localClassName, $controller); array_push($localClassName, ucfirst($action) . 'Data'); $this->localClassName = implode('\\', $localClassName); // calculating controller class name, method and symfony action route $controllerClassName = $path; array_unshift($controllerClassName, Standard::normalizeNamespace($controllersNamespace)); array_push($controllerClassName, $controller . 'Controller'); $controllerClassName = implode('\\', $controllerClassName); if (!class_exists($controllerClassName)) { $this->logger->notice(sprintf('Action: unable to route action "%s" because class "%s" not exists', $requestAction, $controllerClassName)); throw new ApiServerException(sprintf('Action of interface "%s" error: unable to find "%s" class', $this->getInterfaceName(), $controllerClassName)); } $controllerMethodName = $action . 'Action'; if (!method_exists($controllerClassName, $controllerMethodName)) { $this->logger->notice(sprintf('Action: unable to route action "%s" because class "%s" does not has method "%s"', $requestAction, $controllerClassName, $controllerMethodName)); throw new ApiServerException(sprintf('Action of interface "%s" error: controller "%s" has no method "%s"', $this->getInterfaceName(), $controllerClassName, $controllerMethodName)); } $this->actionRoute = $controllerClassName . '::' . $controllerMethodName; $this->logger->debug(sprintf('Action: route set to "%s"', $this->actionRoute)); }
/** * @param $interfaceName * @return ApiServerInterface * @throws ApiServerException */ public function buildInterface($interfaceName) { if (!array_key_exists($interfaceName, $this->config)) { $this->logger->critical(sprintf('Interface factory: no config for interface "%s"', $interfaceName)); throw new ApiServerException(sprintf('Unable to build interface "%s": no config', $interfaceName)); } return new ApiServerInterface($this->container, $this->entityManager, $this->logger, $interfaceName, $this->config[$interfaceName]); }
public function record(Contact $contact) { $this->entityManager->persist($contact); $this->entityManager->flush(); try { $this->eventDispatcher->dispatch(ZigotooEvent::CONTACT, new ContactEvent($contact)); // @codeCoverageIgnoreStart } catch (Exception $e) { $this->logger->critical('', ['exception' => $e, 'contact' => $contact]); } // @codeCoverageIgnoreEnd }
private function authenticate($apiUserToken = null) { if (!$apiUserToken) { $this->apiUser = null; return; } if (!$this->apiUserProviderInterface) { $this->logger->critical('Connection: user auth service is not configured'); throw new ApiServerException(sprintf('Interface "%s" error: try to get user but user auth service is not configured', $this->getInterfaceName())); } $apiToken = $this->decryptToken($apiUserToken); if (!$this->isTokenValid($apiToken)) { $this->logger->notice('Connection: user token is invalid'); throw new ApiServerException(sprintf('Interface "%s" error: invalid user token', $this->getInterfaceName())); } $this->apiUser = $this->apiUserProviderInterface->getApiUser($this->apiClient, $apiToken->getUserId()); if (!$this->apiUser || !$this->apiUser->getId()) { $this->logger->notice('Connection: user token is invalid'); throw new ApiServerException(sprintf('Interface "%s" error: invalid user token', $this->getInterfaceName())); } // checking for renew if ($this->tokenRenewTime) { $now = new \DateTime(); $remain = $now->getTimestamp() - $apiToken->getExpiresAt()->getTimestamp(); if ($remain <= $this->tokenRenewTime) { $this->logger->notice('Connection: time to renew user token'); $this->generateNewApiUserToken(); } } }
private function configureCurl($serverUrl, $timeout = 0) { try { $this->curl = curl_init(); } catch (\Exception $e) { $this->logger->critical(sprintf('Api client %s: unable to initialize curl', $this->getName())); throw $e; } curl_setopt($this->curl, CURLOPT_URL, $serverUrl); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); if ($timeout > 0) { curl_setopt($this->curl, CURLOPT_TIMEOUT, (int) $timeout); } curl_setopt($this->curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($this->curl, CURLOPT_POST, 1); }
private function configureTokenParams($secret, $liveTime, $renewTime) { $this->apiServerCipher = null; if ($this->authService) { if (strlen($secret) < 8) { $this->logger->critical(sprintf('Interface %s: user token secret "%s" is very short, it must be at least 8 symbols', $this->getName(), $secret)); throw new ApiServerException(sprintf('Interface "%s" error: user token secret must be at least 8 symbols (20-40 recommended)', $this->getName())); } $this->apiServerCipher = new ApiServerCipher($secret); } $this->tokenLiveTime = $liveTime; $this->tokenRenewTime = $renewTime; }
public function testCountErrorsWithDebugHandler() { $handler = new DebugHandler(); $logger = new Logger(__METHOD__, array($handler)); $this->assertTrue($logger->debug('test message')); $this->assertTrue($logger->info('test message')); $this->assertTrue($logger->notice('test message')); $this->assertTrue($logger->warning('test message')); $this->assertTrue($logger->error('test message')); $this->assertTrue($logger->critical('test message')); $this->assertTrue($logger->alert('test message')); $this->assertTrue($logger->emergency('test message')); $this->assertSame(4, $logger->countErrors()); }
private function configureUserClass($userClass) { if (!class_exists($userClass)) { $this->logger->critical(sprintf('SecurityManager: user class "%s" is not exists', $userClass)); throw new \Exception(sprintf('Class "%s" not exists', $userClass)); } if (!is_a($userClass, UserInterface::class, true)) { $this->logger->critical(sprintf('SecurityManager: user class "%s" must implements UserCardInterface', $userClass)); throw new \Exception(sprintf('Class "%s" must implements Leoza\\ApiClientBundle\\Client\\UserInterface', $userClass)); } if (!is_a($userClass, Entity::class, true)) { $this->logger->critical(sprintf('SecurityManager: user class "%s" must extends Entity', $userClass)); throw new \Exception(sprintf('Class "%s" must extends Leoza\\EntitiesBundle\\Entity', $userClass)); } $this->userClass = $userClass; }