Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
RepoApiInteractor | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
request | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace ArticlePlaceholder; |
4 | |
5 | use FormatJson; |
6 | use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface; |
7 | use MediaWiki\Http\HttpRequestFactory; |
8 | |
9 | /** |
10 | * Gateway to API of the repository |
11 | * |
12 | * @license GPL-2.0-or-later |
13 | */ |
14 | class RepoApiInteractor { |
15 | |
16 | /** |
17 | * @var string |
18 | */ |
19 | private $repoApiUrl; |
20 | |
21 | /** |
22 | * @var StatsdDataFactoryInterface |
23 | */ |
24 | private $statsdDataFactory; |
25 | |
26 | /** |
27 | * @var HttpRequestFactory |
28 | */ |
29 | private $httpRequestFactory; |
30 | |
31 | /** |
32 | * @param string $repoApiUrl |
33 | * @param StatsdDataFactoryInterface $statsdDataFactory |
34 | * @param HttpRequestFactory $httpRequestFactory |
35 | */ |
36 | public function __construct( |
37 | $repoApiUrl, |
38 | StatsdDataFactoryInterface $statsdDataFactory, |
39 | HttpRequestFactory $httpRequestFactory |
40 | ) { |
41 | $this->repoApiUrl = $repoApiUrl; |
42 | $this->statsdDataFactory = $statsdDataFactory; |
43 | $this->httpRequestFactory = $httpRequestFactory; |
44 | } |
45 | |
46 | /** |
47 | * @param array $params |
48 | * |
49 | * @return array |
50 | */ |
51 | public function request( array $params ) { |
52 | $url = wfAppendQuery( $this->repoApiUrl, $params ); |
53 | $req = $this->httpRequestFactory->create( |
54 | $url, |
55 | [ |
56 | 'userAgent' => 'ArticlePlaceholder ' . $this->httpRequestFactory->getUserAgent(), |
57 | ], |
58 | __METHOD__ |
59 | ); |
60 | |
61 | $status = $req->execute(); |
62 | if ( !$status->isOK() ) { |
63 | $this->statsdDataFactory->increment( 'articleplaceholder.apitermsearch.errored' ); |
64 | return []; |
65 | } |
66 | |
67 | $json = $req->getContent(); |
68 | $data = FormatJson::decode( $json, true ); |
69 | if ( !$data || !empty( $data['error'] ) ) { |
70 | $this->statsdDataFactory->increment( 'articleplaceholder.apitermsearch.invalid' ); |
71 | return []; |
72 | } |
73 | |
74 | $this->statsdDataFactory->increment( 'articleplaceholder.apitermsearch.ok' ); |
75 | return $data; |
76 | } |
77 | |
78 | } |