Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 18
RepoApiInteractor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 18
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 request
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 15
<?php
namespace ArticlePlaceholder;
use FormatJson;
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
use MWHttpRequest;
/**
 * Gateway to API of the repository
 *
 * @license GPL-2.0-or-later
 */
class RepoApiInteractor {
    /**
     * @var string
     */
    private $repoApiUrl;
    /**
     * @var StatsdDataFactoryInterface
     */
    private $statsdDataFactory;
    /**
     * @param string $repoApiUrl
     * @param StatsdDataFactoryInterface $statsdDataFactory
     */
    public function __construct( $repoApiUrl, StatsdDataFactoryInterface $statsdDataFactory ) {
        $this->repoApiUrl = $repoApiUrl;
        $this->statsdDataFactory = $statsdDataFactory;
    }
    /**
     * @param array $params
     *
     * @return array
     */
    public function request( array $params ) {
        $url = wfAppendQuery( $this->repoApiUrl, $params );
        $req = MWHttpRequest::factory(
            $url,
            [],
            __METHOD__
        );
        $status = $req->execute();
        if ( !$status->isOK() ) {
            $this->statsdDataFactory->increment( 'articleplaceholder.apitermsearch.errored' );
            return [];
        }
        $json = $req->getContent();
        $data = FormatJson::decode( $json, true );
        if ( !$data || !empty( $data['error'] ) ) {
            $this->statsdDataFactory->increment( 'articleplaceholder.apitermsearch.invalid' );
            return [];
        }
        $this->statsdDataFactory->increment( 'articleplaceholder.apitermsearch.ok' );
        return $data;
    }
}