Completed
Push — master ( 645e2d...f9b1bd )
by Adam
02:34
created

Server::findService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace PhpJsonRpc\Server\Server;
4
5
use PhpJsonRpc\Server\Service\Service as JsonRpcService;
6
7
/**
8
 * Dummy server.
9
 */
10
class Server
11
{
12
    /**
13
     * @var JsonRpcService[]
14
     */
15
    protected $services = [];
16
17
    /**
18
     * @param JsonRpcService $jsonRpcService
19
     */
20 3
    public function addService(JsonRpcService $jsonRpcService)
21
    {
22 3
        $this->services[$jsonRpcService->endpoint()] = $jsonRpcService;
23 3
    }
24
25
    /**
26
     * @param string $endpoint
27
     * @param string $request
28
     */
29 3
    public function handle($endpoint, $request)
30
    {
31 3
        echo json_encode(
32 3
            $this->findService($endpoint)->dispatch($request)
33 3
        );
34 3
    }
35
36
    /**
37
     * @return JsonRpcService[]
38
     */
39 3
    public function services()
40
    {
41 3
        return $this->services;
42
    }
43
44
    /**
45
     * @param string $endpoint
46
     *
47
     * @return JsonRpcService
48
     *
49
     * @throws \Exception
50
     */
51 3
    public function findService($endpoint)
52
    {
53 3
        if (!array_key_exists($endpoint, $this->services())) {
54
            throw new \Exception("Endpoint <<{$endpoint}>> is not found!");
55 3
        }
56
57 3
        return $this->services()[$endpoint];
58
    }
59
}
60