Ajaxcom::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 2
c 4
b 0
f 2
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
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_FRAGMENT_HEADER = 'HTTP_X_AJAXCOMFRAGMENT';
22
    const AJAX_COM_CACHE_CONTROL = ['Cache-Control' => 'no-cache,max-age=0,must-revalidate,no-store'];
23
24
    /** @var Handler */
25
    private $handler;
26
    /** @var Container */
27
    private $container;
28
29
    public function __construct(Handler $handler, Container $container)
30
    {
31
        $this->handler = $handler;
32
        $this->container = $container;
33
    }
34
35
    public function handle(string $view, array $parameters = []): JsonResponse
36
    {
37
        /** @var MutatorInterface $mutator */
38
        foreach ($this->container->getMutators() as $mutator) {
39
            if ($mutator instanceof RenderableInterface) {
40
                $mutator->setView($view);
41
                $mutator->setParameters($parameters);
42
            }
43
44
            $mutator->mutate($this->handler);
45
        }
46
47
        return new JsonResponse($this->handler->respond(), JsonResponse::HTTP_OK, self::AJAX_COM_CACHE_CONTROL);
48
    }
49
}
50