1 | <?php |
||
5 | trait TokenFromHeaderTrait |
||
6 | { |
||
7 | /** |
||
8 | * Extract the access token from the HTTP request header and |
||
9 | * format it from oauth2 format to return only the token string. |
||
10 | * |
||
11 | * @return string access token |
||
12 | */ |
||
13 | 2 | public function getAccessTokenString() |
|
14 | { |
||
15 | 2 | $request = $this->getRequest(); |
|
|
|||
16 | 2 | $headers = $request->headers->all(); |
|
17 | |||
18 | 2 | return str_replace( |
|
19 | 2 | 'Bearer ', |
|
20 | 2 | '', |
|
21 | 2 | $headers['authorization'] |
|
22 | ); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Check if the user is currently through a token |
||
27 | * linked to allowed client(s) for an action. |
||
28 | * |
||
29 | * @param string $allowedType client type to check |
||
30 | * |
||
31 | * @return bool |
||
32 | * |
||
33 | * @throws AccessDeniedException |
||
34 | */ |
||
35 | 2 | public function throwIfClientNot($allowedType) |
|
48 | |||
49 | /** |
||
50 | * get current by accessToken. |
||
51 | * |
||
52 | * @return User |
||
53 | */ |
||
54 | 2 | public function getCurrentUser() |
|
63 | |||
64 | /** |
||
65 | * |
||
66 | */ |
||
67 | 2 | private function getFOSOauthServer() |
|
71 | } |
||
72 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.