Passed
Pull Request — master (#56)
by Mark
01:52
created

HmacResponseListener::onKernelResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 4
b 0
f 0
nc 3
nop 1
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 3
rs 9.8333
1
<?php
2
3
namespace Acquia\Hmac\Symfony;
4
5
use Acquia\Hmac\ResponseSigner;
6
use Nyholm\Psr7\Factory\Psr17Factory;
7
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
0 ignored issues
show
Bug introduced by
The type Symfony\Bridge\PsrHttpMe...\Factory\PsrHttpFactory 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 Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
0 ignored issues
show
Bug introduced by
The type Symfony\Bridge\PsrHttpMe...y\HttpFoundationFactory 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\HttpKernel\Event\ResponseEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
13
/**
14
 * Signs the response from an HTTP HMAC-authenticated request.
15
 */
16
class HmacResponseListener implements EventSubscriberInterface
17
{
18
    /**
19
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
20 3
     */
21
    public function onKernelResponse(ResponseEvent $event)
22 3
    {
23 1
        if (!$event->isMasterRequest()) {
24
            return;
25
        }
26 2
27 2
        $request = $event->getRequest();
28
        $response = $event->getResponse();
29 2
30 1
        if ($request->attributes->has('hmac.key')) {
31 1
            $psr17Factory = new Psr17Factory();
32
            $httpMessageFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
33 1
            $foundationFactory = new HttpFoundationFactory();
34 1
35
            $psr7Request = $httpMessageFactory->createRequest($request);
36 1
            $psr7Response = $httpMessageFactory->createResponse($response);
37 1
38
            $signer = new ResponseSigner($request->attributes->get('hmac.key'), $psr7Request);
39 1
            $signedResponse = $signer->signResponse($psr7Response);
40
41 2
            $event->setResponse($foundationFactory->createResponse($signedResponse));
42
        }
43
    }
44
45
    /**
46 1
     * {@inheritdoc}
47
     */
48 1
    public static function getSubscribedEvents()
49
    {
50
        return [KernelEvents::RESPONSE => 'onKernelResponse'];
51
    }
52
}
53