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

ChangeUrl   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mutate() 0 14 2
A __construct() 0 4 1
A doNotChangeUrl() 0 5 1
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