Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

Kunstmaan/AdminBundle/Helper/AdminRouteHelper.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper;
4
5
use Kunstmaan\NodeBundle\Router\SlugRouter;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
/**
9
 * Class AdminRouteHelper
10
 */
11
class AdminRouteHelper
12
{
13
    protected static $ADMIN_MATCH_REGEX = '/^\/(app_[a-zA-Z]+\.php\/)?([a-zA-Z_-]{2,5}\/)?%s\/(.*)/';
14
15
    /**
16
     * @var string
17
     */
18
    protected $adminKey;
19
20
    /**
21
     * @var RequestStack
22
     */
23
    protected $requestStack;
24
25
    /**
26
     * @param string       $adminKey
27
     * @param RequestStack $requestStack
28
     */
29
    public function __construct($adminKey, RequestStack $requestStack)
30
    {
31
        $this->adminKey = $adminKey;
32
        $this->requestStack = $requestStack;
33
    }
34
35
    /**
36
     * Checks wether the given url points to an admin route
37
     *
38
     * @param string $url
39
     *
40
     * @return bool
41
     */
42 3
    public function isAdminRoute($url)
43
    {
44 3
        if ($this->matchesPreviewRoute($url)) {
0 ignored issues
show
The call to AdminRouteHelper::matchesPreviewRoute() has too many arguments starting with $url.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45 1
            return false;
46
        }
47
48 2
        preg_match(sprintf(self::$ADMIN_MATCH_REGEX, $this->adminKey), $url, $matches);
49
50
        // Check if path is part of admin area
51 2
        if (\count($matches) === 0) {
52 1
            return false;
53
        }
54
55 1
        return true;
56
    }
57
58
    /**
59
     * Checks the current request if it's route is equal to SlugRouter::$SLUG_PREVIEW
60
     *
61
     * @return bool
62
     */
63
    protected function matchesPreviewRoute()
64
    {
65
        $routeName = $this->requestStack->getCurrentRequest()->get('_route');
66
67
        return $routeName === SlugRouter::$SLUG_PREVIEW;
68
    }
69
}
70