Completed
Pull Request — master (#28)
by
unknown
04:33
created

TokenFromHeaderTrait::getCurrentUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6667
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SMG\ManagerBundle\Controller\Traits;
4
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();
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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)
36
    {
37 2
        $accessToken = $this->getFOSOauthServer()->verifyAccessToken(
38 2
            $this->getAccessTokenString(),
39 2
            'user'
40
        );
41
42 2
        if (!$accessToken->getClient()->isTypeEqualsTo($allowedType)) {
43
            throw $this->createAccessDeniedException(
0 ignored issues
show
Bug introduced by
It seems like createAccessDeniedException() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
44
                'This user type is not allowed for this operation.'
45
            );
46
        }
47 2
    }
48
49
    /**
50
     * get current by accessToken.
51
     *
52
     * @return User
53
     */
54 2
    public function getCurrentUser()
55
    {
56 2
        $accessToken = $this->getFOSOauthServer()->verifyAccessToken(
57 2
            $this->getAccessTokenString(),
58 2
            'user'
59
        );
60
61 2
        return $accessToken->getUser();
62
    }
63
64
    /**
65
     *
66
     */
67 2
    private function getFOSOauthServer()
68
    {
69 2
        return $this->get('fos_oauth_server.server');
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
70
    }
71
}
72