Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
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\Request;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
9
/**
10
 * Class AdminRouteHelper
11
 */
12
class AdminRouteHelper
13
{
14
    protected static $ADMIN_MATCH_REGEX = '/^\/(app_[a-zA-Z]+\.php\/)?([a-zA-Z_-]{2,5}\/)?%s\/(.*)/';
15
16
    /**
17
     * @var string
18
     */
19
    protected $adminKey;
20
21
    /**
22
     * @var RequestStack
23
     */
24
    protected $requestStack;
25
26
    /**
27
     * @param string       $adminKey
28
     * @param RequestStack $requestStack
29
     */
30
    public function __construct($adminKey, RequestStack $requestStack)
31
    {
32
        $this->adminKey = $adminKey;
33
        $this->requestStack = $requestStack;
34
    }
35
36
    /**
37
     * Checks wether the given url points to an admin route
38
     *
39
     * @param string $url
40
     *
41
     * @return bool
42
     */
43 3
    public function isAdminRoute($url)
44
    {
45 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...
46 1
            return false;
47
        }
48
49 2
        preg_match(sprintf(self::$ADMIN_MATCH_REGEX, $this->adminKey), $url, $matches);
50
51
        // Check if path is part of admin area
52 2
        if (count($matches) === 0) {
53 1
            return false;
54
        }
55
56 1
        return true;
57
    }
58
59
    /**
60
     * Checks the current request if it's route is equal to SlugRouter::$SLUG_PREVIEW
61
     *
62
     * @return bool
63
     */
64
    protected function matchesPreviewRoute()
65
    {
66
        $routeName = $this->requestStack->getCurrentRequest()->get('_route');
67
68
        return $routeName === SlugRouter::$SLUG_PREVIEW;
69
    }
70
}
71