Failed Conditions
Pull Request — master (#1399)
by Shinichi
31:45
created

ApplicationTrait::xml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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