aboutsummaryrefslogtreecommitdiffhomepage
path: root/plugins/wallabag/WallabagInstance.php
blob: 88f84ae3a10b5b39140dac1ea7bb7a78ec3b9dd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

namespace Shaarli\Plugin\Wallabag;

/**
 * Class WallabagInstance.
 */
class WallabagInstance
{
    /**
     * @var array Static reference to differrent WB API versions.
     *          - key: version ID, must match plugin settings.
     *          - value: version name.
     */
    private static $wallabagVersions = [
        1 => '1.x',
        2 => '2.x',
    ];

    /**
     * @var array Static reference to WB endpoint according to the API version.
     *          - key: version name.
     *          - value: endpoint.
     */
    private static $wallabagEndpoints = [
        '1.x' => '?plainurl=',
        '2.x' => 'bookmarklet?url=',
    ];

    /**
     * @var string Wallabag user instance URL.
     */
    private $instanceUrl;

    /**
     * @var string Wallabag user instance API version.
     */
    private $apiVersion;

    public function __construct($instance, $version)
    {
        if ($this->isVersionAllowed($version)) {
            $this->apiVersion = self::$wallabagVersions[$version];
        } else {
            // Default API version: 1.x.
            $this->apiVersion = self::$wallabagVersions[1];
        }

        $this->instanceUrl = add_trailing_slash($instance);
    }

    /**
     * Build the Wallabag URL to reach from instance URL and API version endpoint.
     *
     * @return string wallabag url.
     */
    public function getWallabagUrl()
    {
        return $this->instanceUrl . self::$wallabagEndpoints[$this->apiVersion];
    }

    /**
     * Checks version configuration.
     *
     * @param mixed $version given version ID.
     *
     * @return bool true if it's valid, false otherwise.
     */
    private function isVersionAllowed($version)
    {
        return isset(self::$wallabagVersions[$version]);
    }
}