aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/FederationBundle/Federation/CloudId.php
blob: 038ea5e87d1584d16ff7676cb63fb2d29c5b3f59 (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
74
75
76
77
78
<?php

namespace Wallabag\FederationBundle\Federation;

use Wallabag\FederationBundle\Entity\Account;

class CloudId {

    /** @var string */
    private $id;

    /** @var string */
    private $user;

    /** @var string */
    private $remote;

    /**
     * CloudId constructor.
     *
     * @param string $id
     */
    public function __construct($id) {
        $this->id = $id;

        $atPos = strpos($id, '@');
        $user = substr($id, 0, $atPos);
        $remote = substr($id, $atPos + 1);
        if (!empty($user) && !empty($remote)) {
            $this->user = $user;
            $this->remote = $remote;
        }
    }

    /**
     * The full remote cloud id
     *
     * @return string
     */
    public function getId() {
        return $this->id;
    }

    public function getDisplayId() {
        return str_replace('https://', '', str_replace('http://', '', $this->getId()));
    }

    /**
     * The username on the remote server
     *
     * @return string
     */
    public function getUser() {
        return $this->user;
    }

    /**
     * The base address of the remote server
     *
     * @return string
     */
    public function getRemote() {
        return $this->remote;
    }

    /**
     * @param Account $account
     * @param string $domain
     * @return CloudId
     */
    public static function getCloudIdFromAccount(Account $account, $domain = '')
    {
        if ($account->getServer() !== null) {
            return new self($account->getUsername() . '@' . $account->getServer());
        }
        return new self($account->getUsername() . '@' . $domain);
    }
}