@@ -74,7 +74,6 @@ class AuthenticationComponent extends Component implements EventDispatcherInterf
7474 public function initialize (array $ config )
7575 {
7676 $ controller = $ this ->getController ();
77- $ this ->_authentication = $ controller ->request ->getAttribute ('authentication ' );
7877 $ this ->setEventManager ($ controller ->getEventManager ());
7978 }
8079
@@ -85,15 +84,8 @@ public function initialize(array $config)
8584 */
8685 public function beforeFilter ()
8786 {
88- if ($ this ->_authentication === null ) {
89- throw new Exception ('The request object does not contain the required `authentication` attribute ' );
90- }
91-
92- if (!($ this ->_authentication instanceof AuthenticationServiceInterface)) {
93- throw new Exception ('Authentication service does not implement ' . AuthenticationServiceInterface::class);
94- }
95-
96- $ provider = $ this ->_authentication ->getAuthenticationProvider ();
87+ $ authentication = $ this ->getAuthenticationService ();
88+ $ provider = $ authentication ->getAuthenticationProvider ();
9789
9890 if ($ provider === null ||
9991 $ provider instanceof PersistenceInterface ||
@@ -105,10 +97,31 @@ public function beforeFilter()
10597 $ this ->dispatchEvent ('Authentication.afterIdentify ' , [
10698 'provider ' => $ provider ,
10799 'identity ' => $ this ->getIdentity (),
108- 'service ' => $ this -> _authentication
100+ 'service ' => $ authentication
109101 ], $ this ->getController ());
110102 }
111103
104+ /**
105+ * Returns authentication service.
106+ *
107+ * @return \Authentication\AuthenticationServiceInterface
108+ * @throws \Exception
109+ */
110+ public function getAuthenticationService ()
111+ {
112+ $ controller = $ this ->getController ();
113+ $ service = $ controller ->request ->getAttribute ('authentication ' );
114+ if ($ service === null ) {
115+ throw new Exception ('The request object does not contain the required `authentication` attribute ' );
116+ }
117+
118+ if (!($ service instanceof AuthenticationServiceInterface)) {
119+ throw new Exception ('Authentication service does not implement ' . AuthenticationServiceInterface::class);
120+ }
121+
122+ return $ service ;
123+ }
124+
112125 /**
113126 * Start up event handler
114127 *
@@ -181,7 +194,7 @@ public function getUnauthenticatedActions()
181194 */
182195 public function getResult ()
183196 {
184- return $ this ->_authentication ->getResult ();
197+ return $ this ->getAuthenticationService () ->getResult ();
185198 }
186199
187200 /**
@@ -225,7 +238,7 @@ public function setIdentity(ArrayAccess $identity)
225238 {
226239 $ controller = $ this ->getController ();
227240
228- $ result = $ this ->_authentication ->persistIdentity (
241+ $ result = $ this ->getAuthenticationService () ->persistIdentity (
229242 $ controller ->request ,
230243 $ controller ->response ,
231244 $ identity
@@ -247,7 +260,7 @@ public function setIdentity(ArrayAccess $identity)
247260 public function logout ()
248261 {
249262 $ controller = $ this ->getController ();
250- $ result = $ this ->_authentication ->clearIdentity (
263+ $ result = $ this ->getAuthenticationService () ->clearIdentity (
251264 $ controller ->request ,
252265 $ controller ->response
253266 );
0 commit comments