Passed
Pull Request — master (#7)
by
unknown
03:54
created

AjaxcomSymfony4Trait::doNotChangeUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\AjaxcomBundle\Controller;
6
7
use Everlution\AjaxcomBundle\DataObject\Callback;
8
use Everlution\AjaxcomBundle\Mutation;
9
use Everlution\AjaxcomBundle\Service\Ajaxcom;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\HttpFoundation\Response;
12
13
trait AjaxcomSymfony4Trait
14
{
15
    /**
16
     * @var RequestStack
17
     */
18
    private $requestStack;
19
    /**
20
     * @var Ajaxcom
21
     */
22
    private $ajaxcom;
23
    /**
24
     * @var Mutation\AddBlocks
25
     */
26
    private $ajaxcomAddBlocks;
27
    /**
28
     * @var Mutation\RemoveBlocks
29
     */
30
    private $ajaxcomRemoveBlocks;
31
    /**
32
     * @var Mutation\Callbacks
33
     */
34
    private $ajaxcomCallbacks;
35
    /**
36
     * @var Mutation\ReplaceClass
37
     */
38
    private $ajaxcomReplaceClass;
39
    /**
40
     * @var Mutation\ChangeUrl
41
     */
42
    private $ajaxcomChangeUrl;
43
44
    /** @required */
45
    public function setAjaxcomRequiredServices(
46
        RequestStack $requestStack,
47
        Ajaxcom $ajaxcom,
48
        Mutation\AddBlocks $ajaxcomAddBlocks,
49
        Mutation\RemoveBlocks $ajaxcomRemoveBlocks,
50
        Mutation\Callbacks $ajaxcomCallbacks,
51
        Mutation\ReplaceClass $ajaxcomReplaceClass,
52
        Mutation\ChangeUrl $ajaxcomChangeUrl
53
    ): void {
54
        $this->requestStack = $requestStack;
55
        $this->ajaxcom = $ajaxcom;
56
        $this->ajaxcomAddBlocks = $ajaxcomAddBlocks;
57
        $this->ajaxcomRemoveBlocks = $ajaxcomRemoveBlocks;
58
        $this->ajaxcomCallbacks = $ajaxcomCallbacks;
59
        $this->ajaxcomReplaceClass = $ajaxcomReplaceClass;
60
        $this->ajaxcomChangeUrl = $ajaxcomChangeUrl;
61
    }
62
63
    protected function render(string $view, array $parameters = array(), Response $response = null): Response
64
    {
65
        $request = $this->requestStack->getMasterRequest();
66
67
        if ($request->server->get(Ajaxcom::AJAX_COM_HEADER, false)) {
68
            return $this->ajaxcom->handle($view, $parameters);
69
        }
70
71
        return parent::render($view, $parameters, $response);
72
    }
73
74
    protected function renderAjaxBlock(string $id): self
75
    {
76
        $this->ajaxcomAddBlocks->add($id);
77
78
        return $this;
79
    }
80
81
    protected function refreshAjaxBlock(string $id): self
82
    {
83
        $this->ajaxcomAddBlocks->refresh($id);
84
85
        return $this;
86
    }
87
88
    protected function removeAjaxBlock(string $selector): self
89
    {
90
        $this->ajaxcomRemoveBlocks->add($selector);
91
92
        return $this;
93
    }
94
95
    protected function addCallback(string $functionName, array $parameters = []): self
96
    {
97
        $this->ajaxcomCallbacks->add(new Callback($functionName, $parameters));
98
99
        return $this;
100
    }
101
102
    protected function replaceClass(string $selector, string $class): self
103
    {
104
        $this->ajaxcomReplaceClass->add($selector, $class);
105
106
        return $this;
107
    }
108
109
    protected function doNotChangeUrl(): self
110
    {
111
        $this->ajaxcomChangeUrl->doNotChangeUrl();
112
113
        return $this;
114
    }
115
}
116