Completed
Pull Request — experimental/3.1 (#2436)
by Kentaro
58:34
created

ApplicationTrait::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\StreamedResponse;
11
use Symfony\Component\HttpKernel\HttpKernelInterface;
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
15
trait ApplicationTrait
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 118
    public function addSuccess($message, $namespace = 'front')
21
    {
22 118
        $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
    }
24
25 10
    public function addError($message, $namespace = 'front')
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
26
    {
27 10
        $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
    }
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 8
    public function addWarning($message, $namespace = 'front')
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
36
    {
37 8
        $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
    }
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 2
    public function addRequestError($message, $namespace = 'front')
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
46
    {
47 2
        $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 4
    public function clearMessage()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
51
    {
52 4
        $this['session']->getFlashBag()->clear();
53
    }
54
55 4
    public function deleteMessage()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
56
    {
57 4
        $this->clearMessage();
58 4
        $this->addWarning('admin.delete.warning', 'admin');
59
    }
60
61
    public function setLoginTargetPath($targetPath, $namespace = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
62
    {
63
        if (is_null($namespace)) {
64
            $this['session']->getFlashBag()->set('eccube.login.target.path', $targetPath);
65
        } else {
66
            $this['session']->getFlashBag()->set('eccube.' . $namespace . '.login.target.path', $targetPath);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
67
        }
68
    }
69
70 487
    public function isAdminRequest()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
71
    {
72 487
        return isset($this['admin']) ? $this['admin'] : null;
73
    }
74
75 485
    public function isFrontRequest()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
76
    {
77 485
        return isset($this['front']) ? $this['front'] : null;
78
    }
79
80
    /**
81
     * 他のコントローラにリクエストをフォワードします.
82
     *
83
     * @param string $path フォワード先のパス
0 ignored issues
show
introduced by
Expected 14 spaces after parameter name; 1 found
Loading history...
84
     * @param array $requestParameters
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
85
     * @return Response
86
     */
87 38
    public function forward($path, array $requestParameters = [])
88
    {
89 38
        $request = $this['request_stack']->getCurrentRequest();
90
91 38
        $subRequest = Request::create(
92
            $path,
93 38
            $request->getMethod(),
94
            $requestParameters,
95 38
            $request->cookies->all(),
96 38
            [],
97 38
            $request->server->all()
98
        );
99 38
        if ($request->getSession()) {
100 38
            $subRequest->setSession($request->getSession());
101
        }
102
103 38
        return $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
0 ignored issues
show
Bug introduced by
It seems like handle() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
104
    }
105
106
    /**
107
     * フォワードをチェーンでつなげます.
108
     *
109
     * @param string $path フォワード先のパス
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 14 spaces after parameter name; 1 found
Loading history...
110
     * @param array $requestParameters
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
111
     * @param Response $response
112
     * @return Application
113
     */
114
    public function forwardChain($path, array $requestParameters = [], Response &$response = null)
115
    {
116
        $response = $this->forward($path, $requestParameters);
117
        return $this;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
118
    }
119
120
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$key" missing
Loading history...
introduced by
Doc comment for parameter "$service" missing
Loading history...
121
     * コンテナに登録済のサービスを上書きする.
122
     * Pimple標準では再登録を行うと, `RuntimeException: Cannot override frozen service`が投げられるため,一度unsetしてから再登録を行う.
123
     * config系の変更程度に利用はとどめること
124
     *
125
     * @param $key
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
126
     * @param $service
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
127
     * @throws \InvalidArgumentException keyが存在しない場合.
128
     */
129 42
    public function overwrite($key, $service)
130
    {
131 42
        if (!$this->offsetExists($key)) {
0 ignored issues
show
Bug introduced by
It seems like offsetExists() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
132
            throw new \InvalidArgumentException();
133
        }
134 42
        $this->offsetUnset($key);
0 ignored issues
show
Bug introduced by
It seems like offsetUnset() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
135 42
        $this[$key] = $service;
136
    }
137
138
    /**
139
     * プライマリーキーを使用してオブジェクトを取得する.
140
     *
141
     * 主に、マスタデータを取得するために使用する.
142
     *
143
     * @param string $class
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
144
     * @param integer $id
145
     * @throws \InvalidArgumentException $class が不正な場合
146
     * @return object|null エンティティのインスタンス
147
     */
148
    public function find($class, $id)
149
    {
150
        if (!class_exists($class)) {
151
            throw new \InvalidArgumentException();
152
        }
153
        return $this['orm.em']->getRepository($class)->find($id);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
154
    }
155
}
156