/** * get shop by particular event * @param EventAbstract $event * @return \DreamCommerce\ShopAppstoreBundle\Model\ShopInterface */ protected function getShopByEvent(EventAbstract $event) { $params = $event->getPayload(); $appName = $event->getApplicationName(); $shopName = $params['shop']; /** * @var $repo ShopRepositoryInterface */ $repo = $this->objectManager->getRepository('DreamCommerce\\ShopAppstoreBundle\\Model\\ShopInterface'); return $repo->findOneByNameAndApplication($shopName, $appName); }
/** * @param FilterControllerEvent $event * @throws HttpException */ public function onKernelController(FilterControllerEvent $event) { // last event used before token invalid exception is thrown $this->lastEvent = $event; /** * @var $controller ApplicationControllerInterface */ $controller = $event->getController(); /* * $controller passed can be either a class or a Closure. * This is not usual in Symfony but it may happen. * If it is a class, it comes in array format */ if (!is_array($controller)) { return; } // if latest controller on stack is a filtered instance if ($controller[0] instanceof ApplicationControllerInterface) { // get current request data $request = $event->getRequest(); $requestValidator = new RequestValidator($request); try { // get parameters $appName = $requestValidator->getApplicationName($this->applications); $appData = $this->applications[$appName]; $requestValidator->setApplication($appData); $params = $requestValidator->validateAppRequest(); } catch (InvalidRequestException $ex) { // if request is malformed throw new BadRequestHttpException('Invalid request'); } // handle shop arguments on iframe (eg. product list checkboxes) if ($request->query->has('id')) { $ids = $request->query->get('id'); $idsList = @json_decode($ids); $request->query->set('id', $idsList); } // search for installed shop instance by app /** * @var $repo ShopRepositoryInterface */ $repo = $this->objectManager->getRepository('DreamCommerce\\ShopAppstoreBundle\\Model\\ShopInterface'); $shop = $repo->findOneByNameAndApplication($params['shop'], $appName); // not installed - throw an error if (!$shop) { $this->redirect($event, 'not_installed'); } // verify version requirements if ($appData['minimal_version'] > 0) { if ($shop->getVersion() < $appData['minimal_version']) { $this->redirect($event, 'upgrade'); } } // if an application controller needs to be paid if ($controller[0] instanceof PaidControllerInterface) { $billing = $shop->getBilling(); if (empty($billing)) { $this->redirect($event, 'unpaid'); } } // need a subscription? if ($controller[0] instanceof SubscribedControllerInterface) { $subscriptions = $shop->getSubscriptions(); if (!count($subscriptions)) { $this->redirect($event, 'unsubscribed'); } $newest = $subscriptions[0]; $expires = $newest->getExpiresAt(); if ($expires < new \DateTime()) { $this->redirect($event, 'not_installed'); } } // get shop token $token = $shop->getToken(); // instantiate a client /** * @var $client Client\Bearer */ $client = $this->applicationRegistry->get($appName)->getClient($shop); // token expired - attempt to refresh if ($token->getExpiresAt()->getTimestamp() - (new \DateTime())->getTimestamp() < 86400) { $this->refresher->setClient($client); $this->refresher->refresh($shop); } // set token on client $client->setAccessToken($token->getAccessToken()); // action performed on token is invalid $client->setOnTokenInvalidHandler(array($this, 'invalidTokenRedirect')); // pass shop and client $controller[0]->injectClient($client, $shop); // save variables $event->getRequest()->attributes->set('_dream_commerce_shop_appstore_client', $client); $event->getRequest()->attributes->set('_dream_commerce_shop_appstore_shop', $shop); } }