PhpHttp::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
4
namespace Manticoresearch\Transport;
5
6
use Http\Discovery\MessageFactoryDiscovery;
0 ignored issues
show
Bug introduced by
The type Http\Discovery\MessageFactoryDiscovery 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
use Manticoresearch\Connection;
9
use Manticoresearch\Exceptions\ConnectionException;
10
use Manticoresearch\Exceptions\ResponseException;
11
use Manticoresearch\Request;
12
use Manticoresearch\Response;
13
use Manticoresearch\Transport;
14
15
use Http\Discovery\HttpClientDiscovery;
0 ignored issues
show
Bug introduced by
The type Http\Discovery\HttpClientDiscovery 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Psr\Log\LoggerInterface;
17
18
/**
19
 * Class PhpHttp
20
 * @package Manticoresearch\Transport
21
 */
22
class PhpHttp extends Transport implements TransportInterface
23
{
24
    protected $client;
25
    protected $messageFactory;
26
    /**
27
     * PhpHttp constructor.
28
     * @param Connection|null $connection
29
     * @param LoggerInterface|null $logger
30
     */
31
32
    public function __construct(Connection $connection = null, LoggerInterface $logger = null)
33
    {
34
        $this->client = HttpClientDiscovery::find();
35
        $this->messageFactory = MessageFactoryDiscovery::find();
36
        parent::__construct($connection, $logger);
37
    }
38
39
    public function execute(Request $request, $params = [])
40
    {
41
        $connection = $this->getConnection();
42
43
        $url = $this->connection->getConfig('scheme') . '://' . $connection->getHost() . ':' . $connection->getPort()
44
            . $connection->getPath();
45
        $endpoint = $request->getPath();
46
        $url .= $endpoint;
47
        $url = $this->setupURI($url, $request->getQuery());
0 ignored issues
show
Bug introduced by
$request->getQuery() of type string is incompatible with the type array expected by parameter $query of Manticoresearch\Transport::setupURI(). ( Ignorable by Annotation )

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

47
        $url = $this->setupURI($url, /** @scrutinizer ignore-type */ $request->getQuery());
Loading history...
48
        $method = $request->getMethod();
49
50
        $headers = $connection->getHeaders();
51
        $headers['Content-Type'] = $request->getContentType();
52
        $data = $request->getBody();
53
        if (!empty($data)) {
54
            if (is_array($data)) {
55
                $content = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
56
            } else {
57
                $content = $data;
58
            }
59
        } else {
60
            $content = '';
61
        }
62
        $start = microtime(true);
63
        $message = $this->messageFactory->createRequest($method, $url, $headers, $content);
64
        try {
65
            $responsePSR = $this->client->sendRequest($message);
66
        } catch (\Exception $e) {
67
            throw new ConnectionException($e->getMessage(), $request);
68
        }
69
        $end = microtime(true);
70
        $status = $responsePSR->getStatusCode();
71
        $responseString = $responsePSR->getBody();
72
73
        if (isset($params['responseClass'])) {
74
            $responseClass = $params['responseClass'];
75
            $responseClassParams = isset($params['responseClassParams'])?$params['responseClassParams']:[];
76
            $response = new $responseClass($responseString, $status, $responseClassParams);
77
        } else {
78
            $response = new Response($responseString, $status);
79
        }
80
81
        $time = $end-$start;
82
        $response->setTime($time);
83
        $response->setTransportInfo([
84
            'url' => $url,
85
            'headers' => $headers,
86
            'body' => $request->getBody()
87
        ]);
88
        $this->logger->debug('Request body:', [
89
            'connection' => $connection->getConfig(),
90
            'payload'=> $request->getBody()
91
        ]);
92
        $this->logger->info('Request:', [
93
            'url' => $url,
94
            'status' => $status,
95
            'time' => $time,
96
        ]);
97
        $this->logger->debug('Response body:', $response->getResponse());
98
99
        if ($response->hasError()) {
100
            $this->logger->error('Response:', [
101
                'url' => $url,
102
                'error' => $response->getError(),
103
                'payload' => $request->getBody(),
104
            ]);
105
            throw new ResponseException($request, $response);
106
        }
107
        return $response;
108
    }
109
}
110