Completed
Pull Request — master (#1391)
by Kentaro
18:00
created

EccubeExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1.037
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
31
class EccubeExtension extends \Twig_Extension
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
32
{
33
    private $app;
34
35 149
    public function __construct(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
36
    {
37 149
        $this->app = $app;
38
    }
39
40
    /**
41
     * Returns a list of functions to add to the existing list.
42
     *
43
     * @return array An array of functions
44
     */
45 66
    public function getFunctions()
46
    {
47
        return array(
48
            new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')),
49
            new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')),
50
            new \Twig_SimpleFunction('csrf_token_for_anchor', array($this, 'getCsrfTokenForAnchor'), array('is_safe' => array('all'))),
51 66
        );
52
    }
53
54
    /**
55
     * Returns a list of filters.
56
     *
57
     * @return array
58
     */
59 66
    public function getFilters()
60
    {
61
        return array(
62
            new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')),
63
            new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')),
64
            new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')),
65
            new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')),
66
            new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')),
67 66
        );
68
    }
69
70
    /**
71
     * Name of this extension
72
     *
73
     * @return string
74
     */
75 127
    public function getName()
76
    {
77 127
        return 'eccube';
78
    }
79
80
    /**
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...
81
     * Name of this extension
82
     *
83
     * @return string
84
     */
85
    public function getCalcIncTax($price, $tax_rate, $tax_rule)
86
    {
87
        return $price + $this->app['eccube.service.tax_rule']->calcTax($price, $tax_rate, $tax_rule);
88
    }
89
90
    /**
91
     * Name of this extension
92
     *
93
     * @param array $menus
94
     * @return array
95
     */
96
    public function getActiveMenus($menus = array())
97
    {
98
        $count = count($menus);
99
        for ($i = $count; $i <= 2; $i++) {
100
            $menus[] = '';
101
        }
102
103
        return $menus;
104
    }
105
106
    /**
107
     * Name of this extension
108
     *
109
     * @return string
110
     */
111 7
    public function getCsrfTokenForAnchor()
112
    {
113
        $token = $this->app['form.csrf_provider']->getToken(Constant::TOKEN_NAME)->getValue();
114 7
        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...
115
    }
116
117
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$image" missing
Loading history...
118
     * return No Image filename
119
     *
120
     * @return string
121
     */
122 1
    public function getNoImageProduct($image)
123
    {
124 1
        return empty($image) ? 'no_image_product.jpg' : $image;
125
    }
126
127
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
introduced by
Doc comment for parameter "$value" missing
Loading history...
introduced by
Doc comment for parameter "$format" missing
Loading history...
128
     * Name of this extension
129
     *
130
     * @return string
131
     */
132 1
    public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d')
133
    {
134
        if (is_null($date)) {
135 1
            return $value;
136
        } else {
137
            return $date->format($format);
138
        }
139
    }
140
141
    /**
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...
142
     * Name of this extension
143
     *
144
     * @return string
145
     */
146 28
    public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
147
    {
148
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
149 28
        $price = '¥ ' . $price;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
150
151 28
        return $price;
152
    }
153
154
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$value" missing
Loading history...
introduced by
Doc comment for parameter "$length" missing
Loading history...
introduced by
Doc comment for parameter "$end" missing
Loading history...
155
     * Name of this extension
156
     *
157
     * @return string
158
     */
159
    public function getEllipsis($value, $length = 100, $end = '...')
160
    {
161
        return Str::ellipsis($value, $length, $end);
162
    }
163
164
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
165
     * Name of this extension
166
     *
167
     * @return string
168
     */
169
    public function getTimeAgo($date)
170
    {
171
        return Str::timeAgo($date);
172
    }
173
}
174