Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RemoteApiBackend
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 apiCall
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace Flow\Import\LiquidThreadsApi;
4
5use MediaWiki\MediaWikiServices;
6
7class RemoteApiBackend extends ApiBackend {
8    /**
9     * @var string
10     */
11    protected $apiUrl;
12
13    /**
14     * @var string|null
15     */
16    protected $cacheDir;
17
18    /**
19     * @param string $apiUrl
20     * @param string|null $cacheDir
21     */
22    public function __construct( $apiUrl, $cacheDir = null ) {
23        parent::__construct();
24        $this->apiUrl = $apiUrl;
25        $this->cacheDir = $cacheDir;
26    }
27
28    public function getKey() {
29        return $this->apiUrl;
30    }
31
32    public function apiCall( array $params, $retry = 1 ) {
33        $params['format'] = 'json';
34        $params['formatversion'] = 2;
35        $url = wfAppendQuery( $this->apiUrl, $params );
36        $file = $this->cacheDir . '/' . md5( $url ) . '.cache';
37        $this->logger->debug( __METHOD__ . "$url" );
38        if ( $this->cacheDir && file_exists( $file ) ) {
39            $result = file_get_contents( $file );
40        } else {
41            $httpRequestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
42            do {
43                $result = $httpRequestFactory->get( $url, [], __METHOD__ );
44            } while ( $result === null && --$retry >= 0 );
45
46            if ( $this->cacheDir && file_put_contents( $file, $result ) === false ) {
47                $this->logger->warning( "Failed writing cached api result to $file" );
48            }
49        }
50
51        if ( !$result ) {
52            return [];
53        }
54
55        return json_decode( $result, true );
56    }
57}