Completed
Push — master ( 56c44b...57c31b )
by Songda
05:33 queued 11s
created

AbstractProvider::verifyPlugin()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 1
dl 0
loc 9
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Provider;
6
7
use Throwable;
8
use Yansongda\Pay\Contract\HttpClientInterface;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Exception\InvalidParamsException;
11
use Yansongda\Pay\Exception\InvalidResponseException;
12
use Yansongda\Pay\Pay;
13
use Yansongda\Pay\Rocket;
14
use Yansongda\Supports\Collection;
15
use Yansongda\Supports\Pipeline;
16
17
abstract class AbstractProvider
18
{
19
    /**
20
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
21
     * @throws \Yansongda\Pay\Exception\ContainerException
22
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
23
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
24
     *
25
     * @return \Yansongda\Supports\Collection|\Psr\Http\Message\ResponseInterface
26
     */
27
    public function pay(array $plugins, array $params)
28
    {
29
        $this->verifyPlugin($plugins);
30
31
        /* @var Pipeline $pipeline */
32
        $pipeline = Pay::get(Pipeline::class);
33
34
        /* @var Rocket $rocket */
35
        $rocket = $pipeline
36
            ->send((new Rocket())->setParams($params)->setPayload(new Collection()))
37
            ->through($plugins)
38
            ->via('assembly')
39
            ->then(function ($rocket) {
40
                return $this->ignite($rocket);
41
            });
42
43
        return $rocket->getDestination();
44
    }
45
46
    /**
47
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
48
     * @throws \Yansongda\Pay\Exception\ContainerException
49
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
50
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
51
     */
52
    public function ignite(Rocket $rocket): Rocket
53
    {
54
        if (!empty($rocket->getDestination())) {
55
            return $rocket;
56
        }
57
58
        /* @var HttpClientInterface $http */
59
        $http = Pay::get(HttpClientInterface::class);
60
61
        try {
62
            $response = $http->sendRequest($rocket->getRadar());
63
        } catch (Throwable $e) {
64
            throw new InvalidResponseException(InvalidResponseException::REQUEST_RESPONSE_ERROR, $e->getMessage());
65
        }
66
67
        return $rocket->setDestination($response);
68
    }
69
70
    /**
71
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
72
     */
73
    protected function verifyPlugin(array $plugins): void
74
    {
75
        foreach ($plugins as $plugin) {
76
            if ((!is_object($plugin) && !class_exists($plugin)) ||
77
                !in_array(PluginInterface::class, class_implements($plugin))) {
78
                throw new InvalidParamsException(InvalidParamsException::PLUGIN_ERROR, "[$plugin] is not incompatible");
79
            }
80
        }
81
    }
82
}
83