/** * handle installation event * @param InstallEvent $event * @return bool * @throws Exception */ public function onInstall(InstallEvent $event) { // extract shop entity from event $shop = $this->getShopByEvent($event); // already installed, skip if ($shop) { return false; } $shopChecker = new ShopChecker(); try { $params = $event->getPayload(); $app = $event->getApplication(); $url = $shopChecker->getRealShopUrl($params['shop_url']); if (!$url) { throw new Exception('Cannot determine real URL for: ' . $params['shop_url']); } // perform client instantiation $client = Client::factory(Client::ADAPTER_OAUTH, ['entrypoint' => $url, 'client_id' => $app['app_id'], 'client_secret' => $app['app_secret'], 'auth_code' => $params['auth_code'], 'skip_ssl' => $this->skipSsl, 'user_agent' => $app['user_agent']]); // and get tokens $token = $client->authenticate(true); } catch (Exception $ex) { // allow error to be logged throw $ex; } // region shop /** * @var $shopModel ShopInterface */ $shopModel = $this->objectManager->create('DreamCommerce\\ShopAppstoreBundle\\Model\\ShopInterface'); $shopModel->setApp($event->getApplicationName()); $shopModel->setName($params['shop']); $shopModel->setShopUrl($url); $shopModel->setVersion($params['application_version']); $this->objectManager->save($shopModel, false); // endregion // region token /** * @var $tokenModel TokenInterface */ $tokenModel = $this->objectManager->create('DreamCommerce\\ShopAppstoreBundle\\Model\\TokenInterface'); $tokenModel->setAccessToken($token['access_token']); $tokenModel->setRefreshToken($token['refresh_token']); $expiresAt = new \DateTime(); $expiresAt->add(\DateInterval::createFromDateString($token['expires_in'] . ' seconds')); $tokenModel->setExpiresAt($expiresAt); $tokenModel->setShop($shopModel); $this->objectManager->save($tokenModel); // endregion }
/** * get ShopAppstoreLib client * @param ShopInterface $shop * @return ClientInterface * @throws \DreamCommerce\ShopAppstoreLib\Exception\ClientException */ public function getClient(ShopInterface $shop) { $tokens = $shop->getToken(); /** * @var $client Client\OAuth */ $client = Client::factory(Client::ADAPTER_OAUTH, ['entrypoint' => $shop->getShopUrl(), 'client_id' => $this->getAppId(), 'client_secret' => $this->getAppSecret(), 'skip_ssl' => $this->skipSsl, 'user_agent' => $this->getUserAgent()]); $client->setAccessToken($tokens->getAccessToken()); if ($this->logger) { $client->setLogger($this->logger); } return $client; }
/** * {@inheritdoc} */ public function getClient() { if ($this->client === null) { try { $this->client = Client::factory(Client::ADAPTER_OAUTH, array('entrypoint' => $this->entrypoint, 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret)); } catch (ClientException $ex) { throw new HandlerException('Client initialization failed', HandlerException::CLIENT_INITIALIZATION_FAILED, $ex); } } return $this->client; }