PageUrlProvider   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 58.18%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 110
ccs 32
cts 55
cp 0.5818
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 2
A __construct() 0 4 1
A suggest() 0 17 2
A routing() 0 21 4
A all() 0 17 3
A getLabel() 0 6 1
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\PageBundle\Url;
8
9
use Symfony\Component\Routing\RouterInterface;
10
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
11
use Zicht\Bundle\PageBundle\Manager\PageManager;
12
use Zicht\Bundle\PageBundle\Model\PageInterface;
13
use Zicht\Bundle\UrlBundle\Url\AbstractRoutingProvider;
14
use Zicht\Bundle\UrlBundle\Url\ListableProvider;
15
use Zicht\Bundle\UrlBundle\Url\SuggestableProvider;
16
17
/**
18
 * Provides urls for page objects.
19
 */
20
class PageUrlProvider extends AbstractRoutingProvider implements SuggestableProvider, ListableProvider
21
{
22
    /**
23
     * Constructs the provider
24
     *
25
     * @param \Symfony\Component\Routing\RouterInterface $router
26
     * @param \Zicht\Bundle\PageBundle\Manager\PageManager $pageManager
27
     */
28 4
    public function __construct(RouterInterface $router, PageManager $pageManager)
29
    {
30 4
        parent::__construct($router);
31 4
        $this->pageManager = $pageManager;
0 ignored issues
show
Bug Best Practice introduced by
The property pageManager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32 4
    }
33
34
35
    /**
36
     * @{inheritDoc}
37
     */
38 1
    public function supports($object)
39
    {
40 1
        $pageClassName = $this->pageManager->getPageClass();
41 1
        return ($object instanceof $pageClassName) && $object->getId();
42
    }
43
44
45
    /**
46
     * @{inheritDoc}
47
     */
48 2
    public function routing($page, array $options = array())
49
    {
50 2
        if (is_callable(array($page, 'getLanguage')) && $page->getLanguage()) {
51
            if (array_key_exists('_locale', $options)) {
52
                $locale = $options['_locale'];
53
            } else {
54
                $locale = $page->getLanguage();
55
            }
56
57
            return array(
58
                'zicht_page_page_view',
59
                array(
60
                    'id' => $page->getId(),
61
                    '_locale' => $locale,
62
                )
63
            );
64
        } else {
65
            return array(
66 2
                'zicht_page_page_view',
67
                array(
68 2
                    'id' => $page->getId()
69 2
                )
70 2
            );
71
        }
72
    }
73
74
    /**
75
     * @{inheritDoc}
76
     */
77 1
    public function suggest($pattern)
78
    {
79 1
        $pages = $this->pageManager->getBaseRepository()->createQueryBuilder('p')
80 1
            ->andWhere('p.title LIKE :pattern')
81 1
            ->setMaxResults(30)
82 1
            ->getQuery()
83 1
            ->execute(array('pattern' => '%' . $pattern . '%'));
84
85 1
        $suggestions = array();
86 1
        foreach ($pages as $page) {
87 1
            $suggestions[] = array(
88 1
                'value' => $this->url($page),
89 1
                'label' => $this->getLabel($page)
90 1
            );
91 1
        }
92
93 1
        return $suggestions;
94
    }
95
96
    /**
97
     * @{inheritDoc}
98
     */
99
    public function all(AuthorizationCheckerInterface $security)
100
    {
101
        $ret = array();
102
        $pages = $this->pageManager->getBaseRepository()->createQueryBuilder('p')
103
            ->orderBy('p.title')
104
            ->getQuery()
105
            ->execute();
106
107
        foreach ($pages as $page) {
108
            if ($security->isGranted(array('VIEW'), $page)) {
109
                $ret[] = array(
110
                    'value' => $this->url($page),
111
                    'title' => $this->getLabel($page)
112
                );
113
            }
114
        }
115
        return $ret;
116
    }
117
118
    /**
119
     * Returns the label of the page to use in url suggestions
120
     *
121
     * @param \Zicht\Bundle\PageBundle\Model\PageInterface $page
122
     * @return string
123
     */
124 1
    public function getLabel(PageInterface $page)
125
    {
126 1
        return sprintf(
127 1
            '%s (pagina, %s)',
128 1
            $page->getTitle(),
129 1
            $page->getDisplayType()
130 1
        );
131
    }
132
}
133