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\InvalidParamsException |
||
23 | * @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
||
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::make(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 (!should_do_http_request($rocket)) { |
||
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_callable($plugin)) { |
||
77 | continue; |
||
78 | } |
||
79 | |||
80 | if ((is_object($plugin) || |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
81 | (is_string($plugin) && class_exists($plugin))) && |
||
82 | in_array(PluginInterface::class, class_implements($plugin))) { |
||
83 | continue; |
||
84 | } |
||
85 | |||
86 | throw new InvalidParamsException(InvalidParamsException::PLUGIN_ERROR, "[$plugin] is not incompatible"); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 |