MessageFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 45.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 73
rs 10
ccs 11
cts 24
cp 0.4583
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromRequest() 0 7 2
A fromResponse() 0 6 2
A createResponse() 0 5 2
A addIdToRequest() 0 7 3
A createRequest() 0 5 2
1
<?php
2
3
/*
4
 * This file is part of Guzzle HTTP JSON-RPC
5
 *
6
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @see  http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE
12
 * @link http://github.com/graze/guzzle-jsonrpc
13
 */
14
15
namespace Graze\GuzzleHttp\JsonRpc\Message;
16
17
use Graze\GuzzleHttp\JsonRpc;
18
use Psr\Http\Message\RequestInterface as HttpRequestInterface;
19
use Psr\Http\Message\ResponseInterface as HttpResponseInterface;
20
21
class MessageFactory implements MessageFactoryInterface
22
{
23
    /**
24
     * @param string            $method
25
     * @param string            $uri
26
     * @param array             $headers
27
     * @param array             $options
28
     *
29
     * @return RequestInterface
30
     */
31 1
    public function createRequest($method, $uri, array $headers = [], array $options = [])
32
    {
33 1
        $body = JsonRpc\json_encode($this->addIdToRequest($method, $options));
34
35 1
        return new Request('POST', $uri, $headers, $body === false ? null : $body);
36
    }
37
38
    /**
39
     * @param int                $statusCode
40
     * @param array              $headers
41
     * @param array              $options
42
     *
43
     * @return ResponseInterface
44
     */
45 2
    public function createResponse($statusCode, array $headers = [], array $options = [])
46
    {
47 2
        $body = JsonRpc\json_encode($options);
48
49 2
        return new Response($statusCode, $headers, $body === false ? null : $body);
50
    }
51
52
    /**
53
     * @param  HttpRequestInterface $request
54
     *
55
     * @return RequestInterface
56
     */
57
    public function fromRequest(HttpRequestInterface $request)
58
    {
59
        return $this->createRequest(
60
            $request->getMethod(),
61
            $request->getUri(),
62
            $request->getHeaders(),
63
            JsonRpc\json_decode((string) $request->getBody(), true) ?: []
64
        );
65
    }
66
67
    /**
68
     * @param  HttpResponseInterface $response
69
     *
70
     * @return ResponseInterface
71
     */
72
    public function fromResponse(HttpResponseInterface $response)
73
    {
74
        return $this->createResponse(
75
            $response->getStatusCode(),
76
            $response->getHeaders(),
77
            JsonRpc\json_decode((string) $response->getBody(), true) ?: []
78
        );
79
    }
80
81
    /**
82
     * @param  string $method
83
     * @param  array  $data
84
     *
85
     * @return array
86
     */
87 1
    protected function addIdToRequest($method, array $data)
88
    {
89 1
        if (RequestInterface::REQUEST === $method && ! isset($data['id'])) {
90 1
            $data['id'] = uniqid(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $prefix of uniqid(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
            $data['id'] = uniqid(/** @scrutinizer ignore-type */ true);
Loading history...
91 1
        }
92
93 1
        return $data;
94
    }
95
}
96