Failed Conditions
Pull Request — 3.0.9-dev (#1439)
by k-yamamura
57:50 queued 28:34
created

ApplicationTrait::clearMessage()   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
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Eccube\Application;
4
5
use Eccube\Event\TemplateEvent;
6
use Monolog\Logger;
7
use Symfony\Component\Form\FormBuilder;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpFoundation\StreamedResponse;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
/**
14
 * TODO Traitが使えるようになったら不要になる
15
 */
16
class ApplicationTrait extends \Silex\Application
17
{
18
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$message" missing
Loading history...
introduced by
Doc comment for parameter "$namespace" missing
Loading history...
19
     * Application Shortcut Methods
20
     */
21 19
    public function addSuccess($message, $namespace = 'front')
22
    {
23
        $this['session']->getFlashBag()->add('eccube.' . $namespace . '.success', $message);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
24 19
    }
25
26 2
    public function addError($message, $namespace = 'front')
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
27
    {
28
        $this['session']->getFlashBag()->add('eccube.' . $namespace . '.error', $message);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
29 2
    }
30
31
    public function addDanger($message, $namespace = 'front')
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
32
    {
33
        $this['session']->getFlashBag()->add('eccube.' . $namespace . '.danger', $message);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
34
    }
35
36 1
    public function addWarning($message, $namespace = 'front')
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38
        $this['session']->getFlashBag()->add('eccube.' . $namespace . '.warning', $message);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
39 1
    }
40
41
    public function addInfo($message, $namespace = 'front')
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
42
    {
43
        $this['session']->getFlashBag()->add('eccube.' . $namespace . '.info', $message);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
44
    }
45
46
    public function addRequestError($message, $namespace = 'front')
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
47
    {
48
        $this['session']->getFlashBag()->set('eccube.' . $namespace . '.request.error', $message);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
49
    }
50
51
    public function clearMessage()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
52
    {
53
        $this['session']->getFlashBag()->clear();
54
    }
55
56
    public function deleteMessage()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
57
    {
58
        $this->clearMessage();
59
        $this->addWarning('admin.delete.warning', 'admin');
60
    }
61
62
    public function setLoginTargetPath($targetPath)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
63
    {
64
        $this['session']->getFlashBag()->set('eccube.login.target.path', $targetPath);
65
    }
66
67
    public function isAdminRequest()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
68
    {
69
        return $this['admin'];
70
    }
71
72
    public function isFrontRequest()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
73
    {
74
        return $this['front'];
75
    }
76
77
    /*
78
     * 注意!以下コードはSilexのコードのコピーなので触らないコト
79
     *
80
     * 以下のコードの著作権について
81
     *
82
     * (c) Fabien Potencier <[email protected]>
83
     *
84
     * For the full copyright and license information, please view the silex
85
     * LICENSE file that was distributed with this source code.
86
     */
87
88
    /** FormTrait */
89
    /**
90
     * Creates and returns a form builder instance
91
     *
92
     * @param mixed $data The initial data for the form
0 ignored issues
show
introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
93
     * @param array $options Options for the form
94
     *
95
     * @return FormBuilder
96
     */
97
    public function form($data = null, array $options = array())
98
    {
99
        return $this['form.factory']->createBuilder('form', $data, $options);
100
    }
101
102
    /** MonologTrait */
103
    /**
104
     * Adds a log record.
105
     *
106
     * @param string $message The log message
107
     * @param array $context The log context
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
108
     * @param int $level The logging level
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
109
     *
110
     * @return bool Whether the record has been processed
111
     */
112 8
    public function log($message, array $context = array(), $level = Logger::INFO)
113
    {
114
        return $this['monolog']->addRecord($level, $message, $context);
115 8
    }
116
117
    /** SecurityTrait */
118
    /**
119
     * Gets a user from the Security context.
120
     *
121
     * @return mixed
122
     *
123
     * @see TokenInterface::getUser()
124
     *
125
     */
126
    public function user()
127
    {
128
        return $this['user'];
129
    }
130
131
    /**
132
     * Encodes the raw password.
133
     *
134
     * @param UserInterface $user A UserInterface instance
0 ignored issues
show
introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
135
     * @param string $password The password to encode
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
136
     *
137
     * @return string The encoded password
138
     *
139
     * @throws \RuntimeException when no password encoder could be found for the user
140
     */
141 1
    public function encodePassword(UserInterface $user, $password)
142
    {
143
        return $this['security.encoder_factory']->getEncoder($user)->encodePassword($password, $user->getSalt());
144 1
    }
145
146
    /**
147
     * Checks if the attributes are granted against the current authentication token and optionally supplied object.
148
     *
149
     * @param mixed $attributes
150
     * @param mixed $object
151
     *
152
     * @return bool
153
     *
154
     * @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token.
155
     */
156 23
    public function isGranted($attributes, $object = null)
157
    {
158
        return $this['security.authorization_checker']->isGranted($attributes, $object);
159 23
    }
160
161
    /** SwiftmailerTrait */
162
    /**
163
     * Sends an email.
164
     *
165
     * @param \Swift_Message $message A \Swift_Message instance
0 ignored issues
show
introduced by
Expected 10 spaces after parameter name; 1 found
Loading history...
166
     * @param array $failedRecipients An array of failures by-reference
0 ignored issues
show
introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
167
     *
168
     * @return int The number of sent messages
169
     */
170 21
    public function mail(\Swift_Message $message, &$failedRecipients = null)
171
    {
172
        return $this['mailer']->send($message, $failedRecipients);
173 21
    }
174
175
    /** TranslationTrait */
176
    /**
177
     * Translates the given message.
178
     *
179
     * @param string $id The message id
0 ignored issues
show
introduced by
Expected 9 spaces after parameter name; 1 found
Loading history...
180
     * @param array $parameters An array of parameters for the message
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
181
     * @param string $domain The domain for the message
0 ignored issues
show
introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
182
     * @param string $locale The locale
0 ignored issues
show
introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
183
     *
184
     * @return string The translated string
185
     */
186
    public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
187
    {
188
        return $this['translator']->trans($id, $parameters, $domain, $locale);
189
    }
190
191
    /**
192
     * Translates the given choice message by choosing a translation according to a number.
193
     *
194
     * @param string $id The message id
0 ignored issues
show
introduced by
Expected 9 spaces after parameter name; 1 found
Loading history...
195
     * @param int $number The number to use to find the indice of the message
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
196
     * @param array $parameters An array of parameters for the message
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
197
     * @param string $domain The domain for the message
0 ignored issues
show
introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
198
     * @param string $locale The locale
0 ignored issues
show
introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
199
     *
200
     * @return string The translated string
201
     */
202
    public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
203
    {
204
        return $this['translator']->transChoice($id, $number, $parameters, $domain, $locale);
205
    }
206
207
    /** TwigTrait */
208
    /**
209
     * Renders a view and returns a Response.
210
     *
211
     * To stream a view, pass an instance of StreamedResponse as a third argument.
212
     *
213
     * @param string $view The view name
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
214
     * @param array $parameters An array of parameters to pass to the view
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
215
     * @param Response $response A Response instance
0 ignored issues
show
introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
216
     *
217
     * @return Response A Response instance
218
     */
219 63
    public function render($view, array $parameters = array(), Response $response = null)
220
    {
221
        $twig = $this['twig'];
222
223
        // twigファイルのソースコードを読み込み, 文字列化.
224
        $source = $twig->getLoader()->getSource($view);
225
226
        // イベントの実行.
227
        // プラグインにはテンプレートファイル名、文字列化されたtwigファイル、パラメータを渡す
228
        $event = new TemplateEvent($view, $source, $parameters, $response);
229
230 63
        $eventName = $view;
231
        if ($this->isAdminRequest()) {
232
            // 管理画面の場合、event名に「admin.」を付ける
233 60
            $eventName = 'admin.' . $view;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
234
        }
235
        $this['monolog']->debug('Template Event Name : ' . $eventName);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
236
237
        $this['eccube.event.dispatcher']->dispatch($eventName, $event);
238
239 63
        if ($response instanceof StreamedResponse) {
240
            $response->setCallback(function () use ($twig, $view, $parameters) {
241
                $twig->display($view, $parameters);
242
            });
243
        } else {
244 62
            if (null === $response) {
245
                $response = new Response();
246
            }
247
248
            // プラグインで変更された文字列から, テンプレートオブジェクトを生成
249
            $template = $twig->createTemplate($event->getSource());
250
251
            // レンダリング実行.
252
            $content = $template->render($event->getParameters());
253
            $response->setContent($content);
254 1
        }
255
256 63
        return $response;
257 60
    }
258
259
    /**
260
     * Renders a view.
261
     *
262
     * @param string $view The view name
0 ignored issues
show
introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
263
     * @param array $parameters An array of parameters to pass to the view
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
264
     *
265
     * @return string The rendered view
266
     */
267 20
    public function renderView($view, array $parameters = array())
268
    {
269
        return $this['twig']->render($view, $parameters);
270 20
    }
271
272
    /** UrlGeneratorTrait */
273
    /**
274
     * Generates a path from the given parameters.
275
     *
276
     * @param string $route The name of the route
0 ignored issues
show
introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
277
     * @param mixed $parameters An array of parameters
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
278
     *
279
     * @return string The generated path
280
     */
281 42
    public function path($route, $parameters = array())
282
    {
283
        return $this['url_generator']->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH);
284 42
    }
285
286
    /**
287
     * Generates an absolute URL from the given parameters.
288
     *
289
     * @param string $route The name of the route
0 ignored issues
show
introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
290
     * @param mixed $parameters An array of parameters
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
291
     *
292
     * @return string The generated URL
293
     */
294 55
    public function url($route, $parameters = array())
295
    {
296
        return $this['url_generator']->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL);
297 55
    }
298
}
299