Completed
Push — master ( 9260f2...0fae22 )
by Charles
01:40
created

HttpClientComponent::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 22

Duplication

Lines 22
Ratio 70.97 %

Importance

Changes 0
Metric Value
dl 22
loc 31
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace yrc\components;
4
5
use yii\base\BaseObject;
6
use yii\httpclient\Client;
7
use yii\httpclient\Request;
8
use yii\httpclient\RequestEvent;
9
use Yii;
10
11
/**
12
 * Class HttpClientComponent
13
 * Abstracts HTTP requests with request & response logging
14
 * @package app\components
15
 */
16
final class HttpClientComponent extends BaseObject
17
{
18
    /**
19
     * The HTTP Transport to use
20
     * @var string $transport
21
     */
22
    public $transport;
23
24
    /**
25
     * HttpOptions
26
     * @var array $options
27
     */
28
    public $options;
29
30
    /**
31
     * The HTTP client
32
     * @var Client $client
33
     */
34
    private $client;
35
36
    /**
37
     * Returns the client instance
38
     * @return Client
39
     */
40
    public function getClient()
41
    {
42
        return $this->client;
43
    }
44
45
    /**
46
     * Initializes the HttpClient with a transport and a given set of options, and pre-loads events
47
     * @return void
48
     */
49
    public function init()
50
    {
51
        parent::init();
52
        $this->client = new Client([
53
            'transport' => $this->transport
54
        ]);
55
56 View Code Duplication
        $this->client->on(Client::EVENT_BEFORE_SEND, function (RequestEvent $e) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
            Yii::info([
58
                'message' => sprintf('Sending HTTP request [%s] %s', $e->request->getMethod(), $e->request->getUrl()),
59
                'data' => [
60
                    'method' => $e->request->getMethod(),
61
                    'url' => $e->request->getUrl(),
62
                    'data' => $e->request->getData()
63
                ],
64
                'user_id' => Yii::$app->user->id ?? null
65
            ], 'httpclient');
66
        });
67
68 View Code Duplication
        $this->client->on(Client::EVENT_AFTER_SEND, function (RequestEvent $e) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            Yii::info([
70
                'message' => sprintf('Recieved HTTP response HTTP [%s] | [%s] %s', $e->response->getStatusCode(), $e->request->getMethod(), $e->request->getUrl()),
71
                'data' => [
72
                    'method' => $e->request->getData(),
73
                    'url' => $e->request->getUrl(),
74
                    'data' => $e->response->getData()
75
                ],
76
                'user_id' => Yii::$app->user->id ?? null
77
            ], 'httpclient');
78
        });
79
    }
80
}