HmacResponseListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 16
c 5
b 0
f 0
dl 0
loc 36
ccs 16
cts 16
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 3 1
A onKernelResponse() 0 22 4
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;
8
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
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
        $mainRequest = method_exists($event, 'isMainRequest') ? $event->isMainRequest() : $event->isMasterRequest();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpKe...vent::isMasterRequest() has been deprecated: since symfony/http-kernel 5.3, use isMainRequest() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

23
        $mainRequest = method_exists($event, 'isMainRequest') ? $event->isMainRequest() : /** @scrutinizer ignore-deprecated */ $event->isMasterRequest();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
24
        if (!$mainRequest) {
25
            return;
26 2
        }
27 2
28
        $request = $event->getRequest();
29 2
        $response = $event->getResponse();
30 1
31 1
        if ($request->attributes->has('hmac.key')) {
32
            $psr17Factory = new Psr17Factory();
33 1
            $httpMessageFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
34 1
            $foundationFactory = new HttpFoundationFactory();
35
36 1
            $psr7Request = $httpMessageFactory->createRequest($request);
37 1
            $psr7Response = $httpMessageFactory->createResponse($response);
38
39 1
            $signer = new ResponseSigner($request->attributes->get('hmac.key'), $psr7Request);
40
            $signedResponse = $signer->signResponse($psr7Response);
41 2
42
            $event->setResponse($foundationFactory->createResponse($signedResponse));
43
        }
44
    }
45
46 1
    /**
47
     * {@inheritdoc}
48 1
     */
49
    public static function getSubscribedEvents()
50
    {
51
        return [KernelEvents::RESPONSE => 'onKernelResponse'];
52
    }
53
}
54