ChangeUrl   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
dl 0
loc 45
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFragment() 0 3 1
A __construct() 0 4 1
A mutate() 0 18 2
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 Everlution\AjaxcomBundle\Service\Ajaxcom;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
13
/**
14
 * Class ChangeUrl.
15
 *
16
 * @author Ivan Barlog <[email protected]>
17
 */
18
class ChangeUrl implements MutatorInterface
19
{
20
    /** @var Request */
21
    private $request;
22
    /** @var UrlGeneratorInterface */
23
    private $router;
24
    /** @var bool */
25
    private $changeUrl = true;
26
27
    public function __construct(RequestStack $requestStack, UrlGeneratorInterface $router)
28
    {
29
        $this->router = $router;
30
        $this->request = $requestStack->getMainRequest();
31
    }
32
33
    public function mutate(Handler $ajax): Handler
34
    {
35
        if (false === $this->changeUrl) {
36
            return $ajax;
37
        }
38
39
        $ajax->changeUrl(
40
            $this->router->generate(
41
                $this->request->attributes->get('_route'),
42
                array_merge(
43
                    $this->request->attributes->get('_route_params'),
44
                    $this->request->query->all(),
45
                    $this->getFragment()
46
                )
47
            )
48
        );
49
50
        return $ajax;
51
    }
52
53
    public function doNotChangeUrl(): self
54
    {
55
        $this->changeUrl = false;
56
57
        return $this;
58
    }
59
60
    private function getFragment(): array
61
    {
62
        return ['_fragment' => $this->request->server->get(Ajaxcom::AJAX_COM_FRAGMENT_HEADER)];
63
    }
64
}
65