UrlForRedirect::getRedirectUrl()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
3
namespace Application\Bundle\DefaultBundle\Service;
4
5
use JMS\I18nRoutingBundle\Router\I18nRouter;
6
7
/**
8
 * Class UrlForRedirect.
9
 */
10
class UrlForRedirect
11
{
12
    /** @var I18nRouter $router */
13
    protected $router;
14
15
    /** @var $homePages */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $homePages at position 0 could not be parsed: Unknown type name '$homePages' at position 0 in $homePages.
Loading history...
16
    private $homePages = [];
17
    private $authorizationUrls = [];
18
19
    /**
20
     * GetUrlForRedirect constructor.
21
     *
22
     * @param I18nRouter $router
23
     * @param array      $locales
24
     */
25
    public function __construct($router, $locales)
26
    {
27
        $this->router = $router;
28
29
        $this->authorizationUrls[] = $this->router->generate('fos_user_security_login', [], true);
30
        $this->authorizationUrls[] = trim($this->router->generate('fos_user_registration_register', [], true), '\/');
31
        $this->authorizationUrls[] = $this->router->generate('fos_user_resetting_check_email', [], true);
32
        $this->authorizationUrls[] = $this->router->generate('fos_user_resetting_send_email', [], true);
33
        $this->authorizationUrls[] = $this->router->generate('password_already_requested', [], true);
34
35
        $this->homePages[] = trim($router->generate('homepage', [], true), '\/');
36
        $this->homePages[] = trim($router->generate('cabinet', [], true), '\/');
37
        foreach ($locales as $locale) {
38
            $this->homePages[] = $router->generate('homepage', ['_locale' => $locale], true);
39
            $this->homePages[] = $router->generate('cabinet', ['_locale' => $locale], true);
40
41
            $this->authorizationUrls[] = trim($this->router->generate('fos_user_registration_register', ['_locale' => $locale], true), '\/');
42
            $this->authorizationUrls[] = $this->router->generate('fos_user_resetting_check_email', ['_locale' => $locale], true);
43
            $this->authorizationUrls[] = $this->router->generate('fos_user_resetting_send_email', ['_locale' => $locale], true);
44
            $this->authorizationUrls[] = $this->router->generate('password_already_requested', ['_locale' => $locale], true);
45
        }
46
    }
47
48
    /**
49
     * get redirect url for referral url.
50
     *
51
     * @param string $referralUrl
52
     * @param string $host
53
     *
54
     * @return string
55
     */
56
    public function getRedirectUrl($referralUrl, $host = '')
57
    {
58
        $clearReferrer = trim(preg_replace('/(\?.*)/', '', $referralUrl), '\/');
59
60
        if (in_array($clearReferrer, $this->authorizationUrls)) {
61
            return $this->router->generate('homepage');
62
        }
63
64
        if (!empty($host) && false === strpos($clearReferrer, $host)) {
65
            return $this->router->generate('homepage');
66
        }
67
68
        return $referralUrl;
69
    }
70
}
71