Passed
Push — master ( fe64ef...2e5fa1 )
by Ivan
02:01
created

Ajaxcom::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\DataObject\Callback as AjaxCallback;
9
use Everlution\AjaxcomBundle\Mutation\MutatorInterface;
10
use Everlution\AjaxcomBundle\Mutation\RenderableInterface;
11
use Everlution\AjaxcomBundle\Mutation\Container;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
14
/**
15
 * Class Ajaxcom.
16
 *
17
 * @author Ivan Barlog <[email protected]>
18
 */
19
class Ajaxcom
20
{
21
    const AJAX_COM_HEADER = 'HTTP_X_AJAXCOM';
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
    public function renderBlock(string $id): self
51
    {
52
        $this->addBlocks->add($id);
0 ignored issues
show
Bug Best Practice introduced by
The property addBlocks does not exist on Everlution\AjaxcomBundle\Service\Ajaxcom. Did you maybe forget to declare it?
Loading history...
53
54
        return $this;
55
    }
56
57
    public function removeBlock(string $selector): self
58
    {
59
        $this->removeBlocks->add($selector);
0 ignored issues
show
Bug Best Practice introduced by
The property removeBlocks does not exist on Everlution\AjaxcomBundle\Service\Ajaxcom. Did you maybe forget to declare it?
Loading history...
60
61
        return $this;
62
    }
63
64
    public function addCallback(AjaxCallback $callback): self
65
    {
66
        $this->callbacks->add($callback);
0 ignored issues
show
Bug Best Practice introduced by
The property callbacks does not exist on Everlution\AjaxcomBundle\Service\Ajaxcom. Did you maybe forget to declare it?
Loading history...
67
68
        return $this;
69
    }
70
71
    public function replaceClass(string $selector, string $class): self
72
    {
73
        $this->replaceClass->add($selector, $class);
0 ignored issues
show
Bug Best Practice introduced by
The property replaceClass does not exist on Everlution\AjaxcomBundle\Service\Ajaxcom. Did you maybe forget to declare it?
Loading history...
74
75
        return $this;
76
    }
77
78
    public function doNotChangeUrl(): self
79
    {
80
        $this->changeUrl->doNotChangeUrl();
0 ignored issues
show
Bug Best Practice introduced by
The property changeUrl does not exist on Everlution\AjaxcomBundle\Service\Ajaxcom. Did you maybe forget to declare it?
Loading history...
81
82
        return $this;
83
    }
84
}
85