Handler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 137
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
handle() 0 1 ?
A fail() 0 4 1
A toResponse() 0 4 2
A getMessage() 0 19 4
A validate() 0 4 1
A strict() 0 6 3
A getInput() 0 7 1
1
<?php
2
3
namespace Kaylyu\Alipay\F2fpay\Notify;
4
5
use Closure;
6
use Kaylyu\Alipay\Kernel\Exceptions\Exception;
7
8
abstract class Handler
9
{
10
    const SUCCESS = 'success';
11
    const FAILED = 'failed';
12
13
    /**
14
     * @var
15
     * @author kaylv <[email protected]>
16
     */
17
    protected $app;
18
19
    /**
20
     * @var array
21
     */
22
    protected $message;
23
24
    /**
25
     * @var string|null
26
     */
27
    protected $fail;
28
29
    /**
30
     * @var array
31
     */
32
    protected $attributes = [];
33
34
    /**
35
     * Check sign.
36
     * If failed, throws an exception.
37
     *
38
     * @var bool
39
     */
40
    protected $check = true;
41
42
    /**
43
     * Respond with sign.
44
     *
45
     * @var bool
46
     */
47
    protected $sign = true;
48
49
    /**
50
     * Handler constructor.
51
     * @param $app
52
     */
53
    public function __construct($app)
54
    {
55
        $this->app = $app;
56
    }
57
58
    /**
59
     * 处理验签入口
60
     * @param Closure $closure
61
     * @author kaylv <[email protected]>
62
     * @return string
63
     */
64
    abstract public function handle(Closure $closure);
65
66
    /**
67
     * @param string $message
68
     */
69
    public function fail(string $message)
70
    {
71
        $this->fail = $message;
72
    }
73
74
    /**
75
     * 响应数据
76
     * @author kaylv <[email protected]>
77
     * @return string
78
     */
79
    public function toResponse()
80
    {
81
        return is_null($this->fail) ? self::SUCCESS : self::FAILED;
82
    }
83
84
    /**
85
     * 获取参数并验签
86
     * @author kaylv <[email protected]>
87
     * @return array
88
     * @throws Exception
89
     */
90
    public function getMessage(): array
91
    {
92
        if (!empty($this->message)) {
93
            return $this->message;
94
        }
95
96
        //获取参数
97
        $message = $this->getInput();
98
99
        if (empty($message)) {
100
            throw new Exception('Invalid Request.', 400);
101
        }
102
103
        if ($this->check) {
104
            $this->validate($message);
105
        }
106
107
        return $this->message = $message;
108
    }
109
110
    /**
111
     * 验签
112
     * @param $message
113
     * @author kaylv <[email protected]>
114
     * @throws Exception
115
     */
116
    protected function validate($message)
117
    {
118
        throw new Exception('Invalid AopClient.', 400);
119
    }
120
121
    /**
122
     * 处理入口
123
     * @param mixed $result
124
     */
125
    protected function strict($result)
126
    {
127
        if (true !== $result && is_null($this->fail)) {
128
            $this->fail(strval($result));
129
        }
130
    }
131
132
    /**
133
     * 获取参数
134
     * @author kaylv <[email protected]>
135
     * @return array
136
     */
137
    protected function getInput()
138
    {
139
        //获取请求参数
140
        $content = $this->app['request']->all();
141
142
        return $content;
143
    }
144
}
145