protected function fetchPage($pageId, array $params = null, $withBreakpoints = false) { $accessToken = $this->getRequest()->getHeaders()->get('Cookie')->columnis_token; $page = new Page(); $page->setId($pageId); $pageService = $this->getPageService($withBreakpoints); try { if (!$pageService->fetch($page, $params, $accessToken)) { return null; } } catch (PageWithoutTemplateException $e) { } return $page; }
/** * @expectedException \Columnis\Exception\Page\PageWithoutTemplateException */ public function testFetchWithoutTemplate() { $serviceManager = Bootstrap::getServiceManager(); $pageService = $serviceManager->get('Columnis\\Service\\PageService'); /* @var $pageService \Columnis\Service\PageService */ $apiService = $pageService->getApiService(); $plugin = new Mock(); $plugin->addResponse(Bootstrap::getTestFilesDir() . 'api-responses' . DIRECTORY_SEPARATOR . 'generate-invalid.mock'); $mockedClient = $apiService->getHttpClient(); $mockedClient->getEmitter()->attach($plugin); $page = new Page(); $page->setId(1); $pageService->fetch($page); }
/** * Fetchs the page content from Columnis Api * * @param Page $page * @param Array $params * @return boolean */ public function fetch(Page $page, array $queryString = null, $accessToken = null, $retry = 0) { $id = $page->getId(); $endpoint = '/pages/' . $id . '/generate'; $uri = $this->getApiService()->getUri($endpoint); $headers = array('Accept' => 'application/vnd.columnis.v2+json', 'Content-Type' => 'application/json'); if (!empty($accessToken)) { $headers['Authorization'] = sprintf('Bearer %s', $accessToken); } $options = $this->getApiService()->buildOptions(array(), $queryString, $headers); try { $response = $this->getApiService()->request($uri, 'GET', $options); /* @var $response ApiResponse */ $data = $response->getData(); // $legacyData = (new PageLegacyData($data))->getData(); // $data = array_merge($data, $legacyData); //Get page data $dataPagina = array_values($data['columnis.rest.pages'])[0]; $templateService = $this->getTemplateService(); $template = $templateService->createFromData($dataPagina); $pageBreakpointService = $this->getPageBreakpointService(); if (!empty($pageBreakpointService) && is_array($dataPagina) && key_exists('id', $dataPagina)) { $data['pagina']['breakpoint_file'] = $pageBreakpointService->createPageBreakpoint($dataPagina['id'], $data['columnis.rest.configuration'], $data['breakpoints_hash'], $data['collected_pictures'], $data['columnis.rest.image_sizes_groups']); } $data['pagina']['retry'] = $retry; $page->setData($data); $page->setTemplate($template); } catch (UnauthorizedException $e) { $ret = false; if ($retry >= 0) { $retry--; $ret = $this->fetch($page, $params, null, $retry); } $data = $page->getData(); $data['authorization'] = array('error' => $e->getCode(), 'error_description' => $e->getMessage()); $page->setData($data); return $ret; } catch (ApiRequestException $e) { return false; } catch (PathNotFoundException $e) { throw new PageWithoutTemplateException($e->getMessage(), 0, $e); } catch (TemplateNameNotSetException $e) { throw new PageWithoutTemplateException($e->getMessage(), 0, $e); } return true; }