JsonpCallbackMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of coisa/http.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\Http\Middleware;
15
16
use CoiSA\Http\Message\StreamFactory;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Message\StreamFactoryInterface;
20
use Psr\Http\Server\MiddlewareInterface;
21
use Psr\Http\Server\RequestHandlerInterface;
22
23
/**
24
 * Class JsonpCallbackMiddleware
25
 *
26
 * @package CoiSA\Http\Middleware
27
 */
28
final class JsonpCallbackMiddleware implements MiddlewareInterface
29
{
30
    /**
31
     * @const string
32
     */
33
    const REQUEST_QUERY_PARAM = 'callback';
34
35
    /**
36
     * @const string
37
     */
38
    const REQUEST_ACCEPT_HEADER = 'text/javascript';
39
40
    /**
41
     * @var string
42
     */
43
    private $queryParam;
44
45
    /**
46
     * @var StreamFactoryInterface
47
     */
48
    private $streamFactory;
49
50
    /**
51
     * JsonpCallbackMiddleware constructor.
52
     *
53
     * @param string                      $queryParam
54
     * @param null|StreamFactoryInterface $streamFactory
55
     */
56 2
    public function __construct(string $queryParam = self::REQUEST_QUERY_PARAM, StreamFactoryInterface $streamFactory = null)
57
    {
58 2
        $this->queryParam    = $queryParam;
59 2
        $this->streamFactory = $streamFactory ?? new StreamFactory();
60 2
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
66
    {
67 1
        $response = $handler->handle($request);
68 1
        $callback = $request->getQueryParams()[$this->queryParam] ?? false;
69
70 1
        if (!$callback) {
71 1
            return $response;
72
        }
73
74
        if ($request->hasHeader('Accept')
75
            && !\in_array(self::REQUEST_ACCEPT_HEADER, $request->getHeader('Accept'))
76
        ) {
77
            return $response;
78
        }
79
80
        // @TODO accept all types of json content-type ['application/json', 'text/json', 'application/x-json'];
81
82
        if ($response->hasHeader('Content-Type')
83
            && !\in_array('application/json', $response->getHeader('Content-Type'))
84
        ) {
85
            return $response;
86
        }
87
88
        $content = \htmlspecialchars($callback) . '(' . $response->getBody() . ');';
89
        $body    = $this->streamFactory->createStream($content);
90
91
        return $response
92
            ->withHeader('Content-Type', 'text/javascript')
93
            ->withBody($body);
94
    }
95
}
96