Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ShlinkProxy
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 _getProxyUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 _getProxyPayload
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 _extractShortUrl
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php declare(strict_types=1);
2/**
3 * PrivateBin
4 *
5 * a zero-knowledge paste bin
6 *
7 * @link      https://github.com/PrivateBin/PrivateBin
8 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
9 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10 */
11
12namespace PrivateBin\Proxy;
13
14use PrivateBin\Configuration;
15use PrivateBin\Json;
16
17/**
18 * ShlinkProxy
19 *
20 * Forwards a URL for shortening to shlink and stores the result.
21 */
22class ShlinkProxy extends AbstractProxy
23{
24    /**
25     * Overrides the abstract parent function to get the proxy URL.
26     *
27     * @param Configuration $conf
28     * @return string
29     */
30    protected function _getProxyUrl(Configuration $conf): string
31    {
32        return $conf->getKey('apiurl', 'shlink');
33    }
34
35    /**
36     * Overrides the abstract parent function to get contents from Shlink API.
37     *
38     * @access protected
39     * @param Configuration $conf
40     * @param string $link
41     * @return array
42     */
43    protected function _getProxyPayload(Configuration $conf, string $link): array
44    {
45        $shlink_api_key = $conf->getKey('apikey', 'shlink');
46
47        $body = array(
48            'longUrl' => $link,
49        );
50
51        return array(
52            'method'  => 'POST',
53            'header'  => "Content-Type: application/json\r\n" .
54                         'X-Api-Key: ' . $shlink_api_key . "\r\n",
55            'content' => Json::encode($body),
56        );
57    }
58
59    /**
60     * Extracts the short URL from the shlink API response.
61     *
62     * @access protected
63     * @param array $data
64     * @return ?string
65     */
66    protected function _extractShortUrl(array $data): ?string
67    {
68        if (
69            array_key_exists('shortUrl', $data)
70        ) {
71            return $data['shortUrl'];
72        }
73        return null;
74    }
75}