ChangeUrl::getFragment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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 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