Ajaxcom   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 3
Metric Value
wmc 4
eloc 14
c 8
b 1
f 3
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 3
A __construct() 0 4 1
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