AbstractSecureApiController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 46
ccs 8
cts 14
cp 0.5714
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 3 1
A getAuthenticator() 0 14 2
A apiAction() 0 5 1
1
<?php
2
3
namespace Xervice\Api\Communication\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Xervice\Api\Business\Exception\AuthorizationException;
8
use Xervice\Api\Business\Model\Authenticator\ApiAuthenticatorInterface;
9
10
abstract class AbstractSecureApiController extends AbstractApiController
11
{
12
    /**
13
     * @param \Symfony\Component\HttpFoundation\Request $request
14
     * @param string $method
15
     * @param mixed ...$params
16
     *
17
     * @return \Symfony\Component\HttpFoundation\Response
18
     * @throws \Xervice\Api\Business\Exception\ApiException
19
     * @throws \Xervice\Api\Business\Exception\AuthorizationException
20
     */
21 3
    public function apiAction(Request $request, string $method, ...$params): Response
22
    {
23 3
        $this->authorize($request);
24
25
        return parent::apiAction($request, $method, ...$params);
26
    }
27
28
    /**
29
     * @param \Symfony\Component\HttpFoundation\Request $request
30
     *
31
     * @throws \Xervice\Api\Business\Exception\AuthorizationException
32
     */
33 3
    protected function authorize(Request $request): void
34
    {
35 3
        $this->getAuthenticator()->authenticate($request);
36
    }
37
38
    /**
39
     * @return \Xervice\Api\Business\Model\Authenticator\ApiAuthenticatorInterface
40
     * @throws \Xervice\Api\Business\Exception\AuthorizationException
41
     */
42 3
    private function getAuthenticator(): ApiAuthenticatorInterface
43
    {
44 3
        $authenticator = $this->getService('apiAuthenticator');
45
46 3
        if (!$authenticator instanceof ApiAuthenticatorInterface) {
47
            throw new AuthorizationException(
48
                sprintf(
49
                    'No API authenticator service found. %s given',
50
                    \get_class($authenticator)
51
                )
52
            );
53
        }
54
55 3
        return $authenticator;
56
    }
57
}