Passed
Pull Request — master (#662)
by Songda
02:09 queued 23s
created

HtmlResponsePlugin::buildHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
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
use Yansongda\Supports\Collection;
13
14
class HtmlResponsePlugin implements PluginInterface
15
{
16
    public function assembly(Rocket $rocket, Closure $next): Rocket
17
    {
18
        /* @var Rocket $rocket */
19
        $rocket = $next($rocket);
20
21
        Logger::info('[unipay][HtmlResponsePlugin] 插件开始装载', ['rocket' => $rocket]);
22
23
        $radar = $rocket->getRadar();
24
25
        $response = $this->buildHtml($radar->getUri()->__toString(), $rocket->getPayload());
0 ignored issues
show
Bug introduced by
It seems like $rocket->getPayload() can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Uni...onsePlugin::buildHtml() does only seem to accept Yansongda\Supports\Collection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        $response = $this->buildHtml($radar->getUri()->__toString(), /** @scrutinizer ignore-type */ $rocket->getPayload());
Loading history...
26
27
        $rocket->setDestination($response);
28
29
        Logger::info('[unipay][HtmlResponsePlugin] 插件装载完毕', ['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