RouterAware::getRouter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
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 Linio\Controller;
6
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
use Symfony\Component\Routing\RouterInterface;
10
11
trait RouterAware
12
{
13
    /**
14
     * @var RouterInterface
15
     */
16
    protected $router;
17
18
    /**
19
     * @return RouterInterface
20
     */
21
    public function getRouter()
22
    {
23
        return $this->router;
24
    }
25
26
    /**
27
     * @param RouterInterface $router
28
     */
29
    public function setRouter(RouterInterface $router)
30
    {
31
        $this->router = $router;
32
    }
33
34
    /**
35
     * Generates a URL from the given parameters.
36
     *
37
     * @param string      $route         The name of the route
38
     * @param mixed       $parameters    An array of parameters
39
     * @param bool|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
40
     *
41
     * @return string The generated URL
42
     *
43
     * @see UrlGeneratorInterface
44
     */
45
    public function generateUrl($route, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
46
    {
47
        return $this->router->generate($route, $parameters, $referenceType);
48
    }
49
50
    /**
51
     * Returns a RedirectResponse to the given URL.
52
     *
53
     * @param string $url    The URL to redirect to
54
     * @param int    $status The status code to use for the Response
55
     *
56
     * @return RedirectResponse
57
     */
58
    public function redirect($url, $status = 302)
59
    {
60
        return new RedirectResponse($url, $status);
61
    }
62
63
    /**
64
     * Returns a RedirectResponse to the given route with the given parameters.
65
     *
66
     * @param string $route      The name of the route
67
     * @param array  $parameters An array of parameters
68
     * @param int    $status     The status code to use for the Response
69
     *
70
     * @return RedirectResponse
71
     */
72
    protected function redirectToRoute($route, array $parameters = [], $status = 302)
73
    {
74
        return $this->redirect($this->generateUrl($route, $parameters), $status);
75
    }
76
}
77