Passed
Pull Request — master (#56)
by Mark
04:13
created

HmacResponseListener::onKernelResponse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

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