ApiAuthenticator::authenticate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 32
ccs 20
cts 20
cp 1
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
4
namespace Xervice\Api\Business\Model\Authenticator;
5
6
7
use DataProvider\ApiAuthDataProvider;
0 ignored issues
show
Bug introduced by
The type DataProvider\ApiAuthDataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use DataProvider\AuthenticatorDataProvider;
0 ignored issues
show
Bug introduced by
The type DataProvider\AuthenticatorDataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\HttpFoundation\Request;
10
use Xervice\Api\Business\Exception\AuthorizationException;
11
use Xervice\Security\Business\Exception\SecurityException;
12
use Xervice\Security\Business\SecurityFacade;
13
14
class ApiAuthenticator implements ApiAuthenticatorInterface
15
{
16
    /**
17
     * @var \Xervice\Security\Business\SecurityFacade
18
     */
19
    private $securityFacade;
20
21
    /**
22
     * ApiAuthenticator constructor.
23
     *
24
     * @param \Xervice\Security\Business\SecurityFacade $securityFacade
25
     */
26 3
    public function __construct(SecurityFacade $securityFacade)
27
    {
28 3
        $this->securityFacade = $securityFacade;
29 3
    }
30
31
    /**
32
     * @param \Symfony\Component\HttpFoundation\Request $request
33
     *
34
     * @throws \Xervice\Api\Business\Exception\AuthorizationException
35
     */
36 3
    public function authenticate(Request $request)
37
    {
38 3
        $authHeader = $request->headers->get('authorization');
39
40 3
        if (!$authHeader) {
41 1
            throw new AuthorizationException('No HTTP_Authorization header found');
42
        }
43
44 2
        $authParts = explode(' ', $authHeader);
45
46 2
        if (count($authParts) < 2) {
47 1
            throw new AuthorizationException(
48 1
                sprintf(
49 1
                    'Wrong structure for HTTP_Authorization header %s',
50 1
                    $authHeader
51
                )
52
            );
53
        }
54
55 1
        $apiAuth = new ApiAuthDataProvider();
56 1
        $apiAuth->setAuthHeader($authHeader);
57
58 1
        $auth = new AuthenticatorDataProvider();
59 1
        $auth->setAuthData($apiAuth);
60
61
        try {
62 1
            $this->securityFacade->authenticate($authParts[0], $auth);
63 1
        } catch (SecurityException $exception) {
64 1
            throw new AuthorizationException(
65 1
                $exception->getMessage(),
66 1
                0,
67 1
                $exception
68
            );
69
        }
70
    }
71
}