Completed
Push — master ( a3bf41...af2027 )
by FX
03:04
created

AbstractApiController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInvalidTokenView() 0 8 1
A isInvalidToken() 0 5 2
1
<?php
2
3
/**
4
 * Copyright (c) 2017 Francois-Xavier Soubirou.
5
 *
6
 * This file is part of ci-report.
7
 *
8
 * ci-report is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * ci-report is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace AppBundle\Controller;
24
25
use FOS\RestBundle\Controller\FOSRestController;
26
use FOS\RestBundle\View\View;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
30
/**
31
 * API controller class.
32
 *
33
 * @category  ci-report app
34
 *
35
 * @author    Francois-Xavier Soubirou <[email protected]>
36
 * @copyright 2017 Francois-Xavier Soubirou
37
 * @license   http://www.gnu.org/licenses/   GPLv3
38
 *
39
 * @see      https://ci-report.io
40
 */
41
abstract class AbstractApiController extends FOSRestController
42
{
43
    /**
44
     * Check token.
45
     *
46
     * @param Request $request The request
47
     * @param string  $tokenDB The referenced token in database
48
     *
49
     * @return bool
50
     */
51
    protected function isInvalidToken(Request $request, string $tokenDB): bool
52
    {
53
        $tokenRequest = $request->headers->get('X-CIR-TKN');
54
55
        return (null === $tokenRequest) || ($tokenDB !== $tokenRequest);
56
    }
57
58
    /**
59
     * Get invalid token view.
60
     *
61
     * @return View
62
     */
63
    protected function getInvalidTokenView(): View
64
    {
65
        return $this->view(
66
            array(
67
                'code' => Response::HTTP_UNAUTHORIZED,
68
                'message' => 'Invalid token',
69
            ),
70
            Response::HTTP_UNAUTHORIZED
71
        );
72
    }
73
}
74