Page::getUiPageActions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace DoS\ResourceBundle\Twig\Extension;
4
5
use DoS\ResourceBundle\Templating\PageBuilder;
6
use Sonata\SeoBundle\Twig\Extension\SeoExtension;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
use Symfony\Component\Routing\RouterInterface;
10
11
class Page extends \Twig_Extension
12
{
13
    /**
14
     * @var PageBuilder
15
     */
16
    private $pageBuilder;
17
18
    /**
19
     * @var RouterInterface
20
     */
21
    private $router;
22
23
    /**
24
     * @var RequestStack
25
     */
26
    private $request;
27
28
    /**
29
     * @var SeoExtension
30
     */
31
    private $seoExtension;
32
33
    public function __construct(PageBuilder $pageBuilder, SeoExtension $seoExtension = null)
34
    {
35
        $this->pageBuilder = $pageBuilder;
36
        $this->seoExtension = $seoExtension;
37
    }
38
39
    /**
40
     * @param ContainerInterface $container
41
     */
42
    public function setContainer(ContainerInterface $container)
43
    {
44
        $this->router = $container->get('router');
45
        $this->request = $container->get('request_stack');
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function getFunctions()
52
    {
53
        return array_merge(array(
54
            new \Twig_SimpleFunction('ui_page', array($this, 'getUiFile')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
55
            new \Twig_SimpleFunction('ui_page_is', array($this, 'isPageId')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
56
            new \Twig_SimpleFunction('ui_page_get', array($this, 'getPageOption')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
57
            new \Twig_SimpleFunction('ui_page_options', array($this, 'setPageOptions')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
58
            new \Twig_SimpleFunction('ui_page_actions', array($this, 'getUiPageActions')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
59
            new \Twig_SimpleFunction('ui_page_self', array($this, 'getUiPageSelf')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
60
            new \Twig_SimpleFunction('ui_css_if', array($this, 'getCssIfMatchRoute')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
61
        ), $this->seoExtension ? array(
62
            new \Twig_SimpleFunction('ui_seo_title', array($this->seoExtension, 'getTitle'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
63
            new \Twig_SimpleFunction('ui_seo_metadatas', array($this->seoExtension, 'getMetadatas'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
64
            new \Twig_SimpleFunction('ui_seo_html_attributes', array($this->seoExtension, 'getHtmlAttributes'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
65
            new \Twig_SimpleFunction('ui_seo_head_attributes', array($this->seoExtension, 'getHeadAttributes'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
66
            new \Twig_SimpleFunction('ui_seo_link_canonical', array($this->seoExtension, 'getLinkCanonical'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
67
            new \Twig_SimpleFunction('ui_seo_lang_alternates', array($this->seoExtension, 'getLangAlternates'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
68
            new \Twig_SimpleFunction('ui_seo_oembed_links', array($this->seoExtension, 'getOembedLinks'), array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
69
        ) : array());
70
    }
71
72
    /**
73
     * @param $file
74
     * @param \Twig_Template|null $scope
75
     *
76
     * @return string
77
     */
78
    public function getUiFile($file, \Twig_Template $scope = null)
79
    {
80
        if ($scope) {
81
            return $this->getUiPageSelf($scope, $file);
82
        }
83
84
        return sprintf('ui:%s.html.twig', $file);
85
    }
86
87
    /**
88
     * @param \Twig_Template $scope
89
     * @param null|string    $name
90
     *
91
     * @return mixed|string
92
     */
93
    public function getUiPageActions(\Twig_Template $scope, $name = null)
94
    {
95
        $file = $scope->getTemplateName();
96
        $pattern = '/ui\:(.*):(.*)\.html\.twig/';
97
98
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
99
            $file = preg_replace($pattern, sprintf('ui:$1/Actions:%s.html.twig', $name), $file);
100
        } else {
101
            $file = preg_replace($pattern, 'ui:$1/Actions:${2}.html.twig', $file);
102
        }
103
104
        return $file;
105
    }
106
107
    /**
108
     * @param \Twig_Template $scope
109
     * @param string         $name
110
     *
111
     * @return mixed
112
     */
113
    public function getUiPageSelf(\Twig_Template $scope, $name)
114
    {
115
        $file = $scope->getTemplateName();
116
        $pattern = '/ui\:(.*):(.*)\.html\.twig/';
117
118
        return preg_replace($pattern, sprintf('ui:$1:%s.html.twig', $name), $file);
119
    }
120
121
    /**
122
     * @param array $options
123
     */
124
    public function setPageOptions(array $options = array())
125
    {
126
        $this->pageBuilder->setOptions($options);
127
    }
128
129
    /**
130
     * @param string $option
131
     * @param string $printOut
132
     * @param null   $default
133
     *
134
     * @return string
135
     */
136
    public function getPageOption($option, $printOut = '%s', $default = null)
137
    {
138
        return $this->pageBuilder->get($option, $printOut, $default);
139
    }
140
141
    /**
142
     * @param $id
143
     *
144
     * @return bool
145
     */
146
    public function isPageId($id)
147
    {
148
        return $this->pageBuilder->getOption('id') === $id;
149
    }
150
151
    /**
152
     * @param string|bool $check
153
     * @param string      $css
154
     *
155
     * @return null|string
156
     */
157
    public function getCssIfMatchRoute($check, $css = 'active')
158
    {
159
        if (is_string($check)) {
160
            $matcher = $this->router->match($this->request->getCurrentRequest()->getPathInfo());
161
            $check = $matcher['_route'] === $check;
162
        }
163
164
        return $check ? $css : null;
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170
    public function getName()
171
    {
172
        return 'ui_page';
173
    }
174
}
175