DawidMazurek/php-jsonrpc-server

View on GitHub
src/Server/JsonRpcServer.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare(strict_types = 1);

namespace DawidMazurek\JsonRpc\Server;

use DawidMazurek\JsonRpc\Exception\JsonRpcException;
use DawidMazurek\JsonRpc\Handler\JsonRpcRequestHandler;
use DawidMazurek\JsonRpc\IO\JsonRpcInput;
use DawidMazurek\JsonRpc\Response\JsonRpcResponseAggregate;

class JsonRpcServer
{
    private $handler;

    public function __construct(JsonRpcRequestHandler $handler)
    {
        $this->handler = $handler;
    }

    public function run(JsonRpcInput $input): JsonRpcResponseAggregate
    {
        try {
            $responseAggregate = new JsonRpcResponseAggregate();

            foreach ($input->getRequest()->getAll() as $request) {
                $responseAggregate->addResponse(
                    $this->handler->handle($request)
                );
            }
        } catch (JsonRpcException $exception) {
            $responseAggregate->addResponse(
                $this->handler->handleError($exception)
            );
        }

        return $responseAggregate;
    }
}