NoContentViewListener   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 41
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onKernelView() 0 18 6
A createResponse() 0 4 1
1
<?php
2
3
namespace Zenstruck\ControllerUtil\EventListener;
4
5
use Symfony\Component\HttpFoundation\Response;
6
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
7
use Zenstruck\ControllerUtil\View;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
class NoContentViewListener
13
{
14
    private $allowNull;
15
16
    /**
17
     * @param bool $allowNull
18
     */
19 5
    public function __construct($allowNull = true)
20
    {
21 5
        $this->allowNull = $allowNull;
22 5
    }
23
24 5
    public function onKernelView(GetResponseForControllerResultEvent $event)
25
    {
26 5
        $result = $event->getControllerResult();
27
28 5
        if (null === $result && true === $this->allowNull) {
29 1
            $event->setResponse($this->createResponse());
30
31 1
            return;
32
        }
33
34 4
        if (!$result instanceof View) {
35 1
            return;
36
        }
37
38 3
        if (null === $result->getData() && null === $result->getTemplate()) {
39 1
            $event->setResponse($this->createResponse($result->getHeaders()));
40 1
        }
41 3
    }
42
43
    /**
44
     * @param array $headers
45
     *
46
     * @return Response
47
     */
48 2
    private function createResponse(array $headers = array())
49
    {
50 2
        return new Response('', 204, $headers);
51
    }
52
}
53