Completed
Push — master ( bfea6b...805f19 )
by Kentaro
179:04 queued 144:00
created

EccubeExtension::getPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 6
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Twig\Extension;
26
27
use Eccube\Common\Constant;
28
use Eccube\Util\Str;
29
use Silex\Application;
30
use Symfony\Component\Routing\Exception\RouteNotFoundException;
31
32
class EccubeExtension extends \Twig_Extension
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
33
{
34
    private $app;
35 203
36
    public function __construct(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37 203
    {
38
        $this->app = $app;
39
    }
40
41
    /**
42
     * Returns a list of functions to add to the existing list.
43
     *
44
     * @return array An array of functions
45 83
     */
46
    public function getFunctions()
47
    {
48
        $RoutingExtension = $this->app['twig']->getExtension('routing');
49
50
        return array(
51 83
            new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')),
52
            new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')),
53
            new \Twig_SimpleFunction('csrf_token_for_anchor', array($this, 'getCsrfTokenForAnchor'), array('is_safe' => array('all'))),
54
55
            // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::url
56
            new \Twig_SimpleFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))),
57
            // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::path
58
            new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))),
59 83
        );
60
    }
61
62
    /**
63
     * Returns a list of filters.
64
     *
65
     * @return array
66
     */
67 83
    public function getFilters()
68
    {
69
        return array(
70
            new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')),
71
            new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')),
72
            new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')),
73
            new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')),
74
            new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')),
75 175
        );
76
    }
77 175
78
    /**
79
     * Name of this extension
80
     *
81
     * @return string
82
     */
83
    public function getName()
84
    {
85
        return 'eccube';
86
    }
87
88
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$price" missing
Loading history...
introduced by
Doc comment for parameter "$tax_rate" missing
Loading history...
introduced by
Doc comment for parameter "$tax_rule" missing
Loading history...
89
     * Name of this extension
90
     *
91
     * @return string
92
     */
93
    public function getCalcIncTax($price, $tax_rate, $tax_rule)
94
    {
95
        return $price + $this->app['eccube.service.tax_rule']->calcTax($price, $tax_rate, $tax_rule);
96
    }
97
98
    /**
99
     * Name of this extension
100
     *
101
     * @param array $menus
102
     * @return array
103
     */
104
    public function getActiveMenus($menus = array())
105
    {
106
        $count = count($menus);
107
        for ($i = $count; $i <= 2; $i++) {
108
            $menus[] = '';
109
        }
110
111 7
        return $menus;
112
    }
113
114 7
    /**
115
     * Name of this extension
116
     *
117
     * @return string
118
     */
119
    public function getCsrfTokenForAnchor()
120
    {
121
        $token = $this->app['form.csrf_provider']->getToken(Constant::TOKEN_NAME)->getValue();
122 1
        return 'token-for-anchor=\'' . $token . '\'';
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
123
    }
124 1
125
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$image" missing
Loading history...
126
     * return No Image filename
127
     *
128
     * @return string
129
     */
130
    public function getNoImageProduct($image)
131
    {
132 1
        return empty($image) ? 'no_image_product.jpg' : $image;
133
    }
134
135 1
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$format" missing
Loading history...
introduced by
Doc comment for parameter "$date" missing
Loading history...
introduced by
Doc comment for parameter "$value" missing
Loading history...
136
     * Name of this extension
137
     *
138
     * @return string
139
     */
140
    public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d')
141
    {
142
        if (is_null($date)) {
143
            return $value;
144
        } else {
145
            return $date->format($format);
146 31
        }
147
    }
148
149 31
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$number" missing
Loading history...
introduced by
Doc comment for parameter "$decimals" missing
Loading history...
introduced by
Doc comment for parameter "$decPoint" missing
Loading history...
introduced by
Doc comment for parameter "$thousandsSep" missing
Loading history...
150
     * Name of this extension
151 31
     *
152
     * @return string
153
     */
154
    public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
155
    {
156
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
157
        $price = '¥ ' . $price;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
158
159
        return $price;
160
    }
161
162
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$length" missing
Loading history...
introduced by
Doc comment for parameter "$end" missing
Loading history...
introduced by
Doc comment for parameter "$value" missing
Loading history...
163
     * Name of this extension
164
     *
165
     * @return string
166
     */
167
    public function getEllipsis($value, $length = 100, $end = '...')
168
    {
169
        return Str::ellipsis($value, $length, $end);
170
    }
171
172
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
173
     * Name of this extension
174
     *
175
     * @return string
176
     */
177
    public function getTimeAgo($date)
178
    {
179
        return Str::timeAgo($date);
180
    }
181
182
    /**
183
     * bind から URL へ変換します。
184
     * \Symfony\Bridge\Twig\Extension\RoutingExtension::getPath の処理を拡張し、
185
     * RouteNotFoundException 発生時に E_USER_WARNING を発生させ、
186
     * 文字列 "/404?bind={bind}" を返します。
187
     *
188
     * @param string $name
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
189
     * @param array $parameters
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
190
     * @param boolean $relative
191
     * @return string URL
192
     */
193 View Code Duplication
    public function getPath($name, $parameters = array(), $relative = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        $RoutingExtension = $this->app['twig']->getExtension('routing');
196
        try {
197
            return $RoutingExtension->getPath($name, $parameters, $relative);
198
        } catch (RouteNotFoundException $e) {
199
            trigger_error($e->getMessage(), E_USER_WARNING);
200
        }
201
202
        return $RoutingExtension->getPath('homepage').'404?bind='.$name;
203
    }
204
205
    /**
206
     * bind から URL へ変換します。
207
     * \Symfony\Bridge\Twig\Extension\RoutingExtension::getUrl の処理を拡張し、
208
     * RouteNotFoundException 発生時に E_USER_WARNING を発生させ、
209
     * 文字列 "/404?bind={bind}" を返します。
210
     *
211
     * @param string $name
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
212
     * @param array $parameters
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
213
     * @param boolean $schemeRelative
214
     * @return string URL
215
     */
216 View Code Duplication
    public function getUrl($name, $parameters = array(), $schemeRelative = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
    {
218
        $RoutingExtension = $this->app['twig']->getExtension('routing');
219
        try {
220
            return $RoutingExtension->getUrl($name, $parameters, $schemeRelative);
221
        } catch (RouteNotFoundException $e) {
222
            trigger_error($e->getMessage(), E_USER_WARNING);
223
        }
224
225
        return $RoutingExtension->getUrl('homepage').'404?bind='.$name;
226
    }
227
}
228