mingyoung /
easywechat-composer
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the EasyWeChatComposer. |
||
| 7 | * |
||
| 8 | * (c) 张铭阳 <[email protected]> |
||
| 9 | * |
||
| 10 | * This source file is subject to the MIT license that is bundled |
||
| 11 | * with this source code in the file LICENSE. |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace EasyWeChatComposer\Traits; |
||
| 15 | |||
| 16 | use EasyWeChat\Kernel\Http\StreamResponse; |
||
|
0 ignored issues
–
show
|
|||
| 17 | use EasyWeChat\Kernel\Traits\ResponseCastable; |
||
|
0 ignored issues
–
show
The type
EasyWeChat\Kernel\Traits\ResponseCastable was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 18 | use EasyWeChatComposer\Contracts\Encrypter; |
||
| 19 | use EasyWeChatComposer\EasyWeChat; |
||
| 20 | use EasyWeChatComposer\Encryption\DefaultEncrypter; |
||
| 21 | use EasyWeChatComposer\Exceptions\DelegationException; |
||
| 22 | use GuzzleHttp\Client; |
||
|
0 ignored issues
–
show
The type
GuzzleHttp\Client was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 23 | use GuzzleHttp\ClientInterface; |
||
|
0 ignored issues
–
show
The type
GuzzleHttp\ClientInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 24 | |||
| 25 | trait MakesHttpRequests |
||
| 26 | { |
||
| 27 | use ResponseCastable; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \GuzzleHttp\ClientInterface |
||
| 31 | */ |
||
| 32 | protected $httpClient; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \EasyWeChatComposer\Contracts\Encrypter |
||
| 36 | */ |
||
| 37 | protected $encrypter; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $endpoint |
||
| 41 | * @param array $payload |
||
| 42 | * |
||
| 43 | * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
||
|
0 ignored issues
–
show
The type
EasyWeChat\Kernel\Support\Collection was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
The type
Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 44 | * |
||
| 45 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
| 46 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
||
| 47 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 48 | */ |
||
| 49 | protected function request($endpoint, array $payload) |
||
| 50 | { |
||
| 51 | $response = $this->getHttpClient()->request('POST', $endpoint, [ |
||
| 52 | 'form_params' => $this->buildFormParams($payload), |
||
| 53 | ]); |
||
| 54 | |||
| 55 | $parsed = $this->parseResponse($response); |
||
| 56 | |||
| 57 | return $this->detectAndCastResponseToType( |
||
| 58 | $this->getEncrypter()->decrypt($parsed['response']), |
||
| 59 | ($parsed['response_type'] === StreamResponse::class) ? 'raw' : $this->app['config']['response_type'] |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param array $payload |
||
| 65 | * |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | protected function buildFormParams($payload) |
||
| 69 | { |
||
| 70 | return [ |
||
| 71 | 'encrypted' => $this->getEncrypter()->encrypt(json_encode($payload)), |
||
| 72 | ]; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | protected function parseResponse($response) |
||
| 81 | { |
||
| 82 | $result = json_decode((string) $response->getBody(), true); |
||
| 83 | |||
| 84 | if (isset($result['exception'])) { |
||
| 85 | throw (new DelegationException($result['message']))->setException($result['exception']); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $result; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return \GuzzleHttp\ClientInterface |
||
| 93 | */ |
||
| 94 | protected function getHttpClient(): ClientInterface |
||
| 95 | { |
||
| 96 | return $this->httpClient ?: $this->httpClient = new Client([ |
||
| 97 | 'base_uri' => $this->app['config']['delegation']['host'], |
||
| 98 | ]); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return \EasyWeChatComposer\Contracts\Encrypter |
||
| 103 | */ |
||
| 104 | protected function getEncrypter(): Encrypter |
||
| 105 | { |
||
| 106 | return $this->encrypter ?: $this->encrypter = new DefaultEncrypter( |
||
| 107 | EasyWeChat::getEncryptionKey() |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | } |
||
| 111 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths