Passed
Pull Request — master (#913)
by Songda
02:28
created

ResponseHtmlPlugin   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
c 0
b 0
f 0
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildHtml() 0 10 2
A assembly() 0 16 1
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
use Yansongda\Supports\Collection;
13
14
class ResponseHtmlPlugin implements PluginInterface
15
{
16
    public function assembly(Rocket $rocket, Closure $next): Rocket
17
    {
18
        /* @var Rocket $rocket */
19
        $rocket = $next($rocket);
20
21
        Logger::debug('[Unipay][ResponseHtmlPlugin] 插件开始装载', ['rocket' => $rocket]);
22
23
        $radar = $rocket->getRadar();
24
25
        $response = $this->buildHtml($radar->getUri()->__toString(), $rocket->getPayload());
26
27
        $rocket->setDestination($response);
28
29
        Logger::info('[Unipay][ResponseHtmlPlugin] 插件装载完毕', ['rocket' => $rocket]);
30
31
        return $rocket;
32
    }
33
34
    protected function buildHtml(string $endpoint, ?Collection $payload): Response
35
    {
36
        $sHtml = "<form id='pay_form' name='pay_form' action='".$endpoint."' method='POST'>";
37
        foreach ($payload?->all() ?? [] as $key => $val) {
38
            $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
39
        }
40
        $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
41
        $sHtml .= "<script>document.forms['pay_form'].submit();</script>";
42
43
        return new Response(200, [], $sHtml);
44
    }
45
}
46