Handle   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 91
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 10 2
A response() 0 7 2
A __construct() 0 3 1
A getMessage() 0 9 2
1
<?php
2
/*
3
 * This file is part of the wannanbigpig/alipay-sdk-php.
4
 *
5
 * (c) wannanbigpig <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace EasyAlipay\Kernel\Notify;
12
13
use Closure;
14
use EasyAlipay\Kernel\Exceptions\InvalidSignException;
15
use EasyAlipay\Kernel\ServiceContainer;
16
use EasyAlipay\Kernel\Traits\Helpers;
17
use Symfony\Component\HttpFoundation\Response;
18
use WannanBigPig\Supports\Collection;
19
20
/**
21
 * Class Handle
22
 *
23
 * @author   liuml  <[email protected]>
24
 * @DateTime 2019-07-23  17:24
25
 */
26
class Handle
27
{
28
    use Helpers;
29
30
    /**
31
     * @var string
32
     */
33
    protected $fail = 'fail';
34
35
    /**
36
     * @var string
37
     */
38
    protected $success = 'success';
39
40
    /**
41
     * @var array
42
     */
43
    protected $data;
44
45
    /**
46
     * @var string
47
     */
48
    protected $response;
49
50
    /**
51
     * @var \EasyAlipay\Payment\Application
52
     */
53
    protected $app;
54
55
    /**
56
     * Handle constructor.
57
     *
58
     * @param \EasyAlipay\Kernel\ServiceContainer $app
59
     */
60 32
    public function __construct(ServiceContainer $app)
61
    {
62 32
        $this->app = $app;
63 32
    }
64
65
    /**
66
     * run.
67
     *
68
     * @param \Closure $closure
69
     *
70
     * @return \Symfony\Component\HttpFoundation\Response
71
     */
72 32
    public function run(Closure $closure): Response
73
    {
74
        try {
75 32
            $response = \call_user_func($closure, $this->getMessage(), [$this, 'response']);
76 30
        } catch (\Exception $e) {
77 30
            $this->app['logger']->error('Alipay asynchronous notification fatal error:'.$e->getMessage());
78 30
            $response = $this->response(false);
79
        }
80
81 32
        return Response::create($response);
82
    }
83
84
    /**
85
     * response.
86
     *
87
     * @param bool $result
88
     *
89
     * @return string
90
     */
91 32
    public function response(bool $result = true): string
92
    {
93 32
        if ($result === true) {
94 2
            return $this->success;
95
        }
96
97 30
        return $this->fail;
98
    }
99
100
    /**
101
     * getMessage.
102
     *
103
     * @return \WannanBigPig\Supports\Collection
104
     *
105
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
106
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
107
     */
108 32
    public function getMessage(): Collection
109
    {
110 32
        $signData = $request = $this->app['request']->request->all();
111 32
        unset($signData['sign'], $signData['sign_type']);
112 32
        if ($this->verify($this->getSignContentUrlencode($signData), $request['sign'], $request['sign_type'])) {
113 2
            return new Collection($request);
114
        }
115
116 2
        throw new InvalidSignException('Asynchronous notification signature verification failed');
117
    }
118
}
119