changeConfigScope() public method

Switches configuration scope to $siteAccessName and returns the new SiteAccess to use for preview.
public changeConfigScope ( string $siteAccessName ) : SiteAccess
$siteAccessName string
return eZ\Publish\Core\MVC\Symfony\SiteAccess
 /**
  * @throws NotImplementedException If Content is missing location as this is not supported in current version
  */
 public function previewContentAction(Request $request, $contentId, $versionNo, $language, $siteAccessName = null)
 {
     $this->previewHelper->setPreviewActive(true);
     try {
         $content = $this->contentService->loadContent($contentId, array($language), $versionNo);
         $location = $this->locationProvider->loadMainLocation($contentId);
         if (!$location instanceof Location) {
             throw new NotImplementedException("Preview for content without locations");
         }
         $this->previewHelper->setPreviewedContent($content);
         $this->previewHelper->setPreviewedLocation($location);
     } catch (UnauthorizedException $e) {
         throw new AccessDeniedException();
     }
     if (!$this->authorizationChecker->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) {
         throw new AccessDeniedException();
     }
     $siteAccess = $this->previewHelper->getOriginalSiteAccess();
     // Only switch if $siteAccessName is set and different from original
     if ($siteAccessName !== null && $siteAccessName !== $siteAccess->name) {
         $siteAccess = $this->previewHelper->changeConfigScope($siteAccessName);
     }
     $response = $this->kernel->handle($this->getForwardRequest($location, $content, $siteAccess, $request), HttpKernelInterface::SUB_REQUEST);
     $response->headers->remove('cache-control');
     $response->headers->remove('expires');
     $this->previewHelper->restoreConfigScope();
     $this->previewHelper->setPreviewActive(false);
     return $response;
 }
 public function testChangeConfigScope()
 {
     $newSiteAccessName = 'test';
     $newSiteAccess = new SiteAccess($newSiteAccessName, 'preview');
     $event = new ScopeChangeEvent($newSiteAccess);
     $this->eventDispatcher->expects($this->once())->method('dispatch')->with(MVCEvents::CONFIG_SCOPE_CHANGE, $this->equalTo($event));
     $originalSiteAccess = new SiteAccess('foo', 'bar');
     $this->previewHelper->setSiteAccess($originalSiteAccess);
     $this->assertEquals($newSiteAccess, $this->previewHelper->changeConfigScope($newSiteAccessName));
 }
 public function previewContentAction($contentId, $versionNo, $language, $siteAccessName)
 {
     try {
         $content = $this->contentService->loadContent($contentId, array($language), $versionNo);
         $location = $this->previewHelper->getPreviewLocation($contentId);
     } catch (UnauthorizedException $e) {
         throw new AccessDeniedException();
     }
     if (!$this->securityContext->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) {
         throw new AccessDeniedException();
     }
     $newSiteAccess = $this->previewHelper->changeConfigScope($siteAccessName);
     $response = $this->kernel->handle($this->getForwardRequest($location, $content, $newSiteAccess), HttpKernelInterface::SUB_REQUEST);
     $response->headers->remove('cache-control');
     $response->headers->remove('expires');
     $this->previewHelper->restoreConfigScope();
     return $response;
 }
    /**
     * @throws NotImplementedException If Content is missing location as this is not supported in current version
     */
    public function previewContentAction(Request $request, $contentId, $versionNo, $language, $siteAccessName = null)
    {
        $this->previewHelper->setPreviewActive(true);
        try {
            $content = $this->contentService->loadContent($contentId, array($language), $versionNo);
            $location = $this->locationProvider->loadMainLocation($contentId);
            if (!$location instanceof Location) {
                throw new NotImplementedException('Preview for content without locations');
            }
            $this->previewHelper->setPreviewedContent($content);
            $this->previewHelper->setPreviewedLocation($location);
        } catch (UnauthorizedException $e) {
            throw new AccessDeniedException();
        }
        if (!$this->authorizationChecker->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) {
            throw new AccessDeniedException();
        }
        $siteAccess = $this->previewHelper->getOriginalSiteAccess();
        // Only switch if $siteAccessName is set and different from original
        if ($siteAccessName !== null && $siteAccessName !== $siteAccess->name) {
            $siteAccess = $this->previewHelper->changeConfigScope($siteAccessName);
        }
        try {
            $response = $this->kernel->handle($this->getForwardRequest($location, $content, $siteAccess, $request, $language), HttpKernelInterface::SUB_REQUEST, false);
        } catch (\Exception $e) {
            if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
                // @todo This should probably be an exception that embeds the original one
                $message = <<<EOF
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
<p>Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.</p>
EOF;
                throw new Exception($message, 0, $e);
            } else {
                throw $e;
            }
        }
        $response->headers->remove('cache-control');
        $response->headers->remove('expires');
        $this->previewHelper->restoreConfigScope();
        $this->previewHelper->setPreviewActive(false);
        return $response;
    }
 public function testChangeConfigScope()
 {
     $newSiteAccessName = 'test';
     $newSiteAccess = new SiteAccess($newSiteAccessName);
     $this->siteAccessRouter->expects($this->once())->method('matchByName')->with($this->equalTo($newSiteAccessName))->willReturn($newSiteAccess);
     $event = new ScopeChangeEvent($newSiteAccess);
     $this->eventDispatcher->expects($this->once())->method('dispatch')->with(MVCEvents::CONFIG_SCOPE_CHANGE, $this->equalTo($event));
     $originalSiteAccess = new SiteAccess('foo', 'bar');
     $helper = new ContentPreviewHelper($this->contentService, $this->locationService, $this->eventDispatcher, $this->configResolver, $this->siteAccessRouter);
     $helper->setSiteAccess($originalSiteAccess);
     $this->assertEquals($newSiteAccess, $helper->changeConfigScope($newSiteAccessName));
 }