Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
YourlsProxy
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
6
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%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 _extractShortUrl
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
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;
15
16/**
17 * YourlsProxy
18 *
19 * Forwards a URL for shortening to YOURLS (your own URL shortener) and stores
20 * the result.
21 */
22class YourlsProxy 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', 'yourls');
33    }
34
35    /**
36     * Overrides the abstract parent function to get contents from YOURLS 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        return array(
46            'method'  => 'POST',
47            'header'  => "Content-Type: application/x-www-form-urlencoded\r\n",
48            'content' => http_build_query(
49                array(
50                    'signature' => $conf->getKey('signature', 'yourls'),
51                    'format'    => 'json',
52                    'action'    => 'shorturl',
53                    'url'       => $link,
54                )
55            ),
56        );
57    }
58
59    /**
60     * Extracts the short URL from the YOURLS 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('statusCode', $data) &&
70            $data['statusCode'] == 200 &&
71            array_key_exists('shorturl', $data)
72        ) {
73            return $data['shorturl'];
74        }
75        return null;
76    }
77}