carno-php /
hrpcc
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Client dispatcher |
||
| 4 | * User: moyo |
||
| 5 | * Date: 30/09/2017 |
||
| 6 | * Time: 11:04 AM |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace Carno\HRPC\Client; |
||
| 10 | |||
| 11 | use Carno\HRPC\Client\Chips\ErrorsHelper; |
||
| 12 | use Carno\HRPC\Client\Chips\Layers; |
||
| 13 | use Carno\HRPC\Client\Contracts\Defined; |
||
| 14 | use Carno\HTTP\Client; |
||
| 15 | use Carno\HTTP\Standard\Request as HRequest; |
||
| 16 | use Carno\HTTP\Standard\Response as HResponse; |
||
| 17 | use Carno\HTTP\Standard\Streams\Body; |
||
| 18 | use Carno\HTTP\Standard\Uri; |
||
| 19 | use Carno\RPC\Contracts\Client\Cluster; |
||
| 20 | use Carno\RPC\Contracts\Client\Invoker; |
||
| 21 | use Carno\RPC\Exception\RemoteLogicException; |
||
| 22 | use Carno\RPC\Exception\RemoteSystemException; |
||
| 23 | use Carno\RPC\Protocol\Request as RRequest; |
||
| 24 | use Carno\RPC\Protocol\Response as RResponse; |
||
| 25 | |||
| 26 | class Dispatcher implements Invoker |
||
| 27 | { |
||
| 28 | use Layers; |
||
| 29 | use ErrorsHelper; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Clustered |
||
| 33 | */ |
||
| 34 | private $cluster = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Agent |
||
| 38 | */ |
||
| 39 | private $agent = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Dispatcher constructor. |
||
| 43 | * @param Cluster $cluster |
||
| 44 | * @param Agent $agent |
||
| 45 | */ |
||
| 46 | public function __construct(Cluster $cluster, Agent $agent) |
||
| 47 | { |
||
| 48 | $this->cluster = $cluster; |
||
| 49 | $this->agent = $agent; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param RRequest $rpc |
||
| 54 | * @return RResponse |
||
| 55 | */ |
||
| 56 | public function call(RRequest $rpc) |
||
| 57 | { |
||
| 58 | $request = new HRequest( |
||
| 59 | 'POST', |
||
| 60 | new Uri( |
||
| 61 | 'http', |
||
| 62 | $rpc->server(), |
||
| 63 | null, |
||
| 64 | sprintf('/invoke/%s/%s', $rpc->service(), $rpc->method()) |
||
| 65 | ), |
||
| 66 | array_merge( |
||
| 67 | [ |
||
| 68 | 'Host' => $rpc->server(), |
||
| 69 | 'Content-Type' => $rpc->isJsonc() ? Defined::V_TYPE_JSON : Defined::V_TYPE_PROTO, |
||
| 70 | 'User-Agent' => $this->agent->info(), |
||
| 71 | ], |
||
| 72 | $rpc->getExtra('h-headers') ?? [] |
||
| 73 | ), |
||
| 74 | new Body($rpc->getPayload()) |
||
| 75 | ); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Client $client |
||
| 79 | * @var HResponse $response |
||
| 80 | */ |
||
| 81 | |||
| 82 | $client = $rpc->getExtra(Selector::CLI); |
||
| 83 | |||
| 84 | $response = yield $client->perform($request); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 85 | |||
| 86 | if ($this->errorHappened($response)) { |
||
| 87 | throw $response->getStatusCode() === 200 |
||
| 88 | ? new RemoteLogicException(...$this->errorAsParams($response)) |
||
| 89 | : new RemoteSystemException(...$this->errorAsParams($response)) |
||
| 90 | ; |
||
| 91 | } |
||
| 92 | |||
| 93 | return new RResponse($rpc, (string) $response->getBody()); |
||
| 94 | } |
||
| 95 | } |
||
| 96 |