Passed
Pull Request — master (#913)
by Songda
01:55
created

ResponseHtmlPlugin::buildHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Unipay;
6
7
use Closure;
8
use GuzzleHttp\Psr7\Response;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Logger;
11
use Yansongda\Pay\Rocket;
12
13
use function Yansongda\Pay\filter_params;
14
15
class ResponseHtmlPlugin implements PluginInterface
16
{
17
    public function assembly(Rocket $rocket, Closure $next): Rocket
18
    {
19
        /* @var Rocket $rocket */
20
        $rocket = $next($rocket);
21
22
        Logger::debug('[Unipay][ResponseHtmlPlugin] 插件开始装载', ['rocket' => $rocket]);
23
24
        $radar = $rocket->getRadar();
25
        $payload = $rocket->getPayload();
26
27
        $response = $this->buildHtml($radar->getUri()->__toString(), filter_params($payload?->all() ?? []));
28
29
        $rocket->setDestination($response);
30
31
        Logger::info('[Unipay][ResponseHtmlPlugin] 插件装载完毕', ['rocket' => $rocket]);
32
33
        return $rocket;
34
    }
35
36
    protected function buildHtml(string $endpoint, array $payload): Response
37
    {
38
        $sHtml = "<form id='pay_form' name='pay_form' action='".$endpoint."' method='POST'>";
39
        foreach ($payload as $key => $val) {
40
            $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
41
        }
42
        $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
43
        $sHtml .= "<script>document.forms['pay_form'].submit();</script>";
44
45
        return new Response(200, [], $sHtml);
46
    }
47
}
48