Passed
Push — master ( d78e5e...e129d0 )
by Ivan
02:47
created

Ajaxcom::renderBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\AjaxcomBundle\Service;
6
7
use Everlution\Ajaxcom\Handler;
8
use Everlution\AjaxcomBundle\Mutation\MutatorInterface;
9
use Everlution\AjaxcomBundle\Mutation\RenderableInterface;
10
use Everlution\AjaxcomBundle\Mutation\Container;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
13
/**
14
 * Class Ajaxcom.
15
 *
16
 * @author Ivan Barlog <[email protected]>
17
 */
18
class Ajaxcom
19
{
20
    const AJAX_COM_HEADER = 'HTTP_X_AJAXCOM';
21
    const AJAX_COM_CACHE_CONTROL = ['Cache-Control' => 'no-cache,max-age=0,must-revalidate,no-store'];
22
23
    /** @var Handler */
24
    private $handler;
25
    /** @var Container */
26
    private $container;
27
28
    public function __construct(Handler $handler, Container $container)
29
    {
30
        $this->handler = $handler;
31
        $this->container = $container;
32
    }
33
34
    public function handle(string $view, array $parameters = []): JsonResponse
35
    {
36
        /** @var MutatorInterface $mutator */
37
        foreach ($this->container->getMutators() as $mutator) {
38
            if ($mutator instanceof RenderableInterface) {
39
                $mutator->setView($view);
40
                $mutator->setParameters($parameters);
41
            }
42
43
            $mutator->mutate($this->handler);
44
        }
45
46
        return new JsonResponse($this->handler->respond(), JsonResponse::HTTP_OK, self::AJAX_COM_CACHE_CONTROL);
47
    }
48
}
49