Completed
Push — experimental/3.1 ( c83f5d...923e5e )
by Yangsin
63:41 queued 57:20
created

EccubeExtension::getPriceFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 1
rs 9.4285
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\Bridge\Twig\Extension\RoutingExtension;
31
use Symfony\Component\Routing\Exception\RouteNotFoundException;
32
33
class EccubeExtension extends \Twig_Extension
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
34
{
35
    private $app;
36
37 1178
    public function __construct(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
38
    {
39 1178
        $this->app = $app;
40
    }
41
42
    /**
43
     * Returns a list of functions to add to the existing list.
44
     *
45
     * @return array An array of functions
46
     */
47 96
    public function getFunctions()
48
    {
49 96
        $RoutingExtension = $this->app['twig']->getExtension(RoutingExtension::class);
50
51
        $app = $this->app;
52 96
        return array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
introduced by
Add a comma after each item in a multi-line array
Loading history...
53 96
            new \Twig_SimpleFunction('is_object', array($this, 'isObject')),
54 96
            new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')),
55 96
            new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')),
56
            new \Twig_SimpleFunction('csrf_token_for_anchor', array($this, 'getCsrfTokenForAnchor'), array('is_safe' => array('all'))),
57
58 96
            // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::url
59
            new \Twig_SimpleFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))),
60 96
            // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::path
61
            new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))),
62
63
            new \Twig_SimpleFunction('php_*', function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
64
                    $arg_list = func_get_args();
65
                    $function = array_shift($arg_list);
66
                    if (is_callable($function)) {
67
                        return call_user_func_array($function, $arg_list);
68
                    }
69 96
                    trigger_error('Called to an undefined function : php_'. $function, E_USER_WARNING);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
70
71
            }, ['pre_escape' => 'html', 'is_safe' => ['html']]),
72 96
73 96
            new \Twig_SimpleFunction('eccube_block_*', function() use ($app) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
74 96
                    $sources = $app['eccube.twig.block.templates'];
75 96
                    $arg_list = func_get_args();
76 96
                    $block_name = array_shift($arg_list);
77
                    foreach ($sources as $source) {
78
                        $template = $app['twig']->loadTemplate($source);
79
                        if (!isset($arg_list[0])) {
80
                            $arg_list[0] = [];
81
                        }
82
                        if ($template->hasBlock($block_name, $arg_list[0])) {
83
                            echo $result = $template->renderBlock($block_name, $arg_list[0]);
84
                            return;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
85 1178
                        }
86
                    }
87 1178
                    trigger_error($block_name.' block is not found', E_USER_WARNING);
88
            }, ['pre_escape' => 'html', 'is_safe' => ['html']])
89
        );
90
    }
91
92
    /**
93
     * Returns a list of filters.
94
     *
95 10
     * @return array
96
     */
97 10
    public function getFilters()
98
    {
99
        return array(
100
            new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')),
101
            new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')),
102
            new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')),
103
            new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')),
104
            new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')),
105
        );
106 142
    }
107
108 142
    public function getTokenParsers()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
109 142
    {
110 87
        return $this->app['twig_parsers'];
111
    }
112
113 142
    /**
114
     * Name of this extension
115
     *
116
     * @return string
117
     */
118
    public function getName()
119
    {
120
        return 'eccube';
121 27
    }
122
123 27
    /**
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...
124 27
     * Name of this extension
125
     *
126
     * @return string
127
     */
128
    public function getCalcIncTax($price, $tax_rate, $tax_rule)
129
    {
130
        return $price + $this->app['eccube.service.tax_rule']->calcTax($price, $tax_rate, $tax_rule);
131
    }
132 8
133
    /**
134 8
     * Name of this extension
135
     *
136
     * @param array $menus
137
     * @return array
138
     */
139
    public function getActiveMenus($menus = array())
140
    {
141
        $count = count($menus);
142 14
        for ($i = $count; $i <= 2; $i++) {
143
            $menus[] = '';
144 14
        }
145 10
146
        return $menus;
147 4
    }
148
149
    /**
150
     * Name of this extension
151
     *
152
     * @return string
153
     */
154
    public function getCsrfTokenForAnchor()
155
    {
156 87
        $token = $this->app['csrf.token_manager']->getToken(Constant::TOKEN_NAME)->getValue();
157
        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...
158 87
    }
159 87
160
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$image" missing
Loading history...
161 87
     * return No Image filename
162
     *
163
     * @return string
164
     */
165
    public function getNoImageProduct($image)
166
    {
167
        return empty($image) ? 'no_image_product.jpg' : $image;
168
    }
169
170
    /**
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...
171
     * Name of this extension
172
     *
173
     * @return string
174
     */
175
    public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d')
176
    {
177
        if (is_null($date)) {
178
            return $value;
179
        } else {
180
            return $date->format($format);
181
        }
182
    }
183
184
    /**
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...
185
     * Name of this extension
186
     *
187
     * @return string
188
     */
189
    public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
190
    {
191
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
192
        $price = '¥ ' . $price;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
193
194
        return $price;
195 225
    }
196
197 225
    /**
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...
198
     * Name of this extension
199 225
     *
200
     * @return string
201
     */
202
    public function getEllipsis($value, $length = 100, $end = '...')
203
    {
204
        return Str::ellipsis($value, $length, $end);
205
    }
206
207
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
208
     * Name of this extension
209
     *
210
     * @return string
211
     */
212
    public function getTimeAgo($date)
213
    {
214
        return Str::timeAgo($date);
215
    }
216
217
    /**
218 216
     * bind から URL へ変換します。
219
     * \Symfony\Bridge\Twig\Extension\RoutingExtension::getPath の処理を拡張し、
220 216
     * RouteNotFoundException 発生時に E_USER_WARNING を発生させ、
221
     * 文字列 "/404?bind={bind}" を返します。
222 216
     *
223
     * @param string $name
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
224
     * @param array $parameters
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
225
     * @param boolean $relative
226
     * @return string URL
227
     */
228 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...
229
    {
230
        $RoutingExtension = $this->app['twig']->getExtension(RoutingExtension::class);
231
        try {
232
            return $RoutingExtension->getPath($name, $parameters, $relative);
233
        } catch (RouteNotFoundException $e) {
234
            trigger_error($e->getMessage(), E_USER_WARNING);
235
        }
236 2
237
        return $RoutingExtension->getPath('homepage').'404?bind='.$name;
238 2
    }
239
240
    /**
241
     * bind から URL へ変換します。
242
     * \Symfony\Bridge\Twig\Extension\RoutingExtension::getUrl の処理を拡張し、
243
     * RouteNotFoundException 発生時に E_USER_WARNING を発生させ、
244
     * 文字列 "/404?bind={bind}" を返します。
245
     *
246
     * @param string $name
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
247
     * @param array $parameters
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
248
     * @param boolean $schemeRelative
249
     * @return string URL
250
     */
251 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...
252
    {
253
        $RoutingExtension = $this->app['twig']->getExtension(RoutingExtension::class);
254
        try {
255
            return $RoutingExtension->getUrl($name, $parameters, $schemeRelative);
256
        } catch (RouteNotFoundException $e) {
257
            trigger_error($e->getMessage(), E_USER_WARNING);
258
        }
259
260
        return $RoutingExtension->getUrl('homepage').'404?bind='.$name;
261
    }
262
263
    /**
264
     * Check if the value is object
265
     *
266
     * @param object $value
267
     * @return bool
268
     */
269
    public function isObject($value)
270
    {
271
        return is_object($value);
272
    }
273
}
274