RequireAjaxMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 43
ccs 9
cts 9
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 7 2
A createForbiddenResponse() 0 6 1
1
<?php namespace Neomerx\Limoncello\Http\Middleware;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \Closure;
20
use \Symfony\Component\HttpFoundation\Request;
21
use \Symfony\Component\HttpFoundation\Response;
22
use \Neomerx\Limoncello\Contracts\IntegrationInterface;
23
24
/**
25
 * @package Neomerx\Limoncello
26
 */
27
class RequireAjaxMiddleware
28
{
29
    /**
30
     * @var IntegrationInterface
31
     */
32
    private $integration;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param IntegrationInterface $integration
38
     */
39 2
    public function __construct(IntegrationInterface $integration)
40
    {
41 2
        $this->integration = $integration;
42 2
    }
43
44
    /**
45
     * Handle an incoming request.
46
     *
47
     * @param Request $request
48
     * @param Closure $next
49
     *
50
     * @return Response
51
     */
52 2
    public function handle(Request $request, Closure $next)
53
    {
54
        /** @var Response $response */
55 2
        $response = $request->isXmlHttpRequest() === true ? $next($request) : $this->createForbiddenResponse();
56
57 2
        return $response;
58
    }
59
60
    /**
61
     * @return Response
62
     */
63 1
    protected function createForbiddenResponse()
64
    {
65 1
        $response = $this->integration->createResponse(null, Response::HTTP_FORBIDDEN, []);
66
67 1
        return $response;
68
    }
69
}
70