@@ -15,12 +15,19 @@ class Client
15
15
{
16
16
public const API_BASE_PATH = 'https://api-metrika.yandex.ru ' ;
17
17
18
+ /**
19
+ * @var array<string, mixed>
20
+ */
18
21
private array $ config ;
19
22
20
23
private ClientInterface $ httpClient ;
21
24
22
25
private LoggerInterface $ logger ;
23
26
27
+ /**
28
+ * @param array<string, mixed> $config
29
+ * @throws ClientException
30
+ */
24
31
public function __construct (array $ config = [])
25
32
{
26
33
$ this ->config = array_merge ([
@@ -35,36 +42,71 @@ public function __construct(array $config = [])
35
42
$ this ->httpClient = $ this ->createDefaultHttpClient ();
36
43
}
37
44
45
+ /**
46
+ * Set AUTH token
47
+ *
48
+ * @param string $token
49
+ * @return void
50
+ */
38
51
public function setToken (string $ token ): void
39
52
{
40
53
$ this ->config ['token ' ] = $ token ;
41
54
}
42
55
56
+ /**
57
+ * Set logger
58
+ *
59
+ * @param LoggerInterface $logger
60
+ * @return void
61
+ */
43
62
public function setLogger (LoggerInterface $ logger ): void
44
63
{
45
64
$ this ->logger = $ logger ;
46
65
}
47
66
67
+ /**
68
+ * Get logger
69
+ *
70
+ * @return LoggerInterface
71
+ */
48
72
public function getLogger (): LoggerInterface
49
73
{
50
74
return $ this ->logger ;
51
75
}
52
76
77
+ /**
78
+ * Set HTTP client
79
+ *
80
+ * @param ClientInterface $httpClient
81
+ * @return void
82
+ */
53
83
public function setHttpClient (ClientInterface $ httpClient ): void
54
84
{
55
85
$ this ->httpClient = $ httpClient ;
56
86
}
57
87
88
+ /**
89
+ * Get HTTP client
90
+ *
91
+ * @return ClientInterface
92
+ */
58
93
public function getHttpClient (): ClientInterface
59
94
{
60
95
return $ this ->httpClient ;
61
96
}
62
97
98
+ /**
99
+ * Execute HTTP request
100
+ *
101
+ * @param RequestInterface $request
102
+ * @return array<mixed>
103
+ * @throws ClientException
104
+ */
63
105
public function execute (RequestInterface $ request ): array
64
106
{
65
107
try {
66
108
$ request = $ request ->withHeader ('Authorization ' , sprintf ('OAuth %s ' , $ this ->config ['token ' ]));
67
- $ response = $ this ->httpClient ->sendRequest ($ request );
109
+ $ response = $ this ->httpClient ->send ($ request );
68
110
} catch (ClientExceptionInterface $ e ) {
69
111
$ this ->getLogger ()->error ($ e ->getMessage ());
70
112
@@ -74,6 +116,11 @@ public function execute(RequestInterface $request): array
74
116
return json_decode ($ response ->getBody (), true );
75
117
}
76
118
119
+ /**
120
+ * Create default logger
121
+ *
122
+ * @return LoggerInterface
123
+ */
77
124
protected function createDefaultLogger (): LoggerInterface
78
125
{
79
126
$ logger = new Logger ('ya-metrika-php-client ' );
@@ -83,6 +130,11 @@ protected function createDefaultLogger(): LoggerInterface
83
130
return $ logger ;
84
131
}
85
132
133
+ /**
134
+ * Create default HTTP client
135
+ *
136
+ * @return ClientInterface
137
+ */
86
138
protected function createDefaultHttpClient (): ClientInterface
87
139
{
88
140
return new GuzzleClient ([
0 commit comments