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

ChangeUrl::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\Mutation;
6
7
use Everlution\Ajaxcom\Handler;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
12
/**
13
 * Class ChangeUrl.
14
 *
15
 * @author Ivan Barlog <[email protected]>
16
 */
17
class ChangeUrl implements MutatorInterface
18
{
19
    /** @var Request */
20
    private $request;
21
    /** @var UrlGeneratorInterface */
22
    private $router;
23
    /** @var bool */
24
    private $changeUrl = true;
25
26
    public function __construct(RequestStack $requestStack, UrlGeneratorInterface $router)
27
    {
28
        $this->router = $router;
29
        $this->request = $requestStack->getMasterRequest();
30
    }
31
32
    public function mutate(Handler $ajax): Handler
33
    {
34
        if (false === $this->changeUrl) {
35
            return $ajax;
36
        }
37
38
        $ajax->changeUrl(
39
            $this->router->generate(
40
                $this->request->attributes->get('_route'),
41
                array_merge($this->request->attributes->get('_route_params'), $this->request->query->all())
42
            )
43
        );
44
45
        return $ajax;
46
    }
47
48
    public function doNotChangeUrl(): self
49
    {
50
        $this->changeUrl = false;
51
52
        return $this;
53
    }
54
}
55