Test Failed
Push — release/4.x ( d1eb98...528038 )
by
unknown
04:34
created

UrlExtension::shortUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Bundle\UrlBundle\Twig;
7
8
use Twig\TwigFunction;
9
use Zicht\Bundle\UrlBundle\Exception\UnsupportedException;
10
use Zicht\Bundle\UrlBundle\Url\ShortUrlManager;
11
12
/**
13
 * Provides some twig utilities.
14
 */
15
class UrlExtension extends \Twig_Extension
16
{
17
    protected $provider;
18
    protected $aliasing;
19
20
    /**
21
     * @var ShortUrlManager
22
     */
23
    private $shortUrlManager;
24
25
    /**
26
     * Construct the extension with the passed object as provider. The provider is typically a DelegatingProvider
27
     * that delegates to all registered url providers.
28
     *
29
     * @param \Zicht\Bundle\UrlBundle\Url\Provider $provider
30
     * @param ShortUrlManager $shortUrlManager
31
     * @param \Zicht\Bundle\UrlBundle\Aliasing\Aliasing $aliasing
32
     */
33
    public function __construct($provider, ShortUrlManager $shortUrlManager, $aliasing = null)
34
    {
35
        $this->provider = $provider;
36
        $this->aliasing = $aliasing;
37
        $this->shortUrlManager = $shortUrlManager;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getFilters()
44
    {
45
        return [
46
            new \Twig_SimpleFilter('internal_to_public_aliasing', [$this, 'internalToPublicAliasing']),
47
        ];
48
    }
49
50
    /**
51
     * Takes a HTML sting and replaces all internal urls with aliased public urls, i.e. /nl/page/42 -> /nl/bring-your-towel
52
     *
53
     * @param string $html
54
     * @return string
55
     *
56
     * @deprecated Should no longer be used, the aliasing is now handled by a response listener.
57
     */
58
    public function internalToPublicAliasing($html)
59
    {
60
        return $html;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getFunctions()
67
    {
68
        return [
69
            'object_url' => new \Twig_SimpleFunction('object_url', [$this, 'objectUrl']),
70
            'static_ref' => new \Twig_SimpleFunction('static_ref', [$this, 'staticRef']),
71
            'static_reference' => new \Twig_SimpleFunction('static_reference', [$this, 'staticRef']),
72
            'short_url' => new TwigFunction('short_url', [$this, 'shortUrl'])
73
        ];
74
    }
75
76
    /**
77
     * Returns an url based on the passed object.
78
     *
79
     * @param object $object
80
     * @param mixed $defaultIfNotFound
81
     * @return string
82
     */
83
    public function objectUrl($object, $defaultIfNotFound = null)
84
    {
85
        try {
86
            $ret = $this->provider->url($object);
87
        } catch (UnsupportedException $e) {
88
            if (null === $defaultIfNotFound) {
89
                throw $e;
90
            } else {
91
                if (true === $defaultIfNotFound) {
92
                    $ret = (string)$object;
93
                } else {
94
                    $ret = $defaultIfNotFound;
95
                }
96
            }
97
        }
98
        return $ret;
99
    }
100
101
    /**
102
     * Returns a static reference, i.e. an url that is provided based on a simple string.
103
     *
104
     * @param string $name
105
     * @param array $params
106
     *
107
     * @return string
108
     */
109
    public function staticRef($name, $params = null)
110
    {
111
        $name = (string)$name;
112
        if (!isset($this->static_refs[$name])) {
113
            try {
114
                $this->static_refs[$name] = $this->provider->url($name);
115
            } catch (UnsupportedException $e) {
116
                $this->static_refs[$name] = '/[static_reference: ' . $name . ']';
117
            }
118
        }
119
120
        $ret = $this->static_refs[$name];
121
        if ($params) {
122
            $ret .= '?' . http_build_query($params, 0, '&');
123
        }
124
125
        return $ret;
126
    }
127
128
    private $static_refs = [];
129
130
    /**
131
     * @param string $originatingUrl
132
     * @param string|null $prefix
133
     * @param int $minLength
134
     * @return string
135
     */
136
    public function shortUrl($originatingUrl, $prefix = null, $minLength = 8)
137
    {
138
        $alias = $this->shortUrlManager->getAlias($originatingUrl, $prefix, $minLength);
139
        return $alias->getPublicUrl();
140
    }
141
142
    /**
143
     * Returns the name of the extension.
144
     *
145
     * @return string The extension name
146
     */
147
    public function getName()
148
    {
149
        return 'zicht_url';
150
    }
151
}
152