3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Controller\Plugin
;
12 use Zend\Authentication\AuthenticationServiceInterface
;
13 use Zend\Mvc\Exception
;
16 * Controller plugin to fetch the authenticated identity.
18 class Identity
extends AbstractPlugin
21 * @var AuthenticationServiceInterface
23 protected $authenticationService;
26 * @return AuthenticationServiceInterface
28 public function getAuthenticationService()
30 return $this->authenticationService
;
34 * @param AuthenticationServiceInterface $authenticationService
36 public function setAuthenticationService(AuthenticationServiceInterface
$authenticationService)
38 $this->authenticationService
= $authenticationService;
42 * Retrieve the current identity, if any.
44 * If none is present, returns null.
47 * @throws Exception\RuntimeException
49 public function __invoke()
51 if (!$this->authenticationService
instanceof AuthenticationServiceInterface
) {
52 throw new Exception\
RuntimeException('No AuthenticationServiceInterface instance provided');
54 if (!$this->authenticationService
->hasIdentity()) {
57 return $this->authenticationService
->getIdentity();