Completed
Branch master (133319)
by Luca
01:20
created

PreferenceModel::getUserId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * This Driver is based entirely on official documentation of the Mattermost Web
4
 * Services API and you can extend it by following the directives of the documentation.
5
 *
6
 * For the full copyright and license information, please read the LICENSE.txt
7
 * file that was distributed with this source code. For the full list of
8
 * contributors, visit https://github.com/gnello/php-mattermost-driver/contributors
9
 *
10
 * God bless this mess.
11
 *
12
 * @author Luca Agnello <[email protected]>
13
 * @link https://api.mattermost.com/
14
 */
15
16
namespace Gnello\Mattermost\Models;
17
18
use Gnello\Mattermost\Client;
19
use Psr\Http\Message\ResponseInterface;
20
21
/**
22
 * Class PreferenceModel
23
 *
24
 * @package Gnello\Mattermost\Models
25
 */
26
class PreferenceModel extends AbstractModel
27
{
28
    /**
29
     * @var string
30
     */
31
    public static $endpoint = '/preferences';
32
33
    /**
34
     * @var string
35
     */
36
    private $userId;
37
38
    /**
39
     * PreferenceModel constructor.
40
     *
41
     * @param Client $client
42
     */
43
    public function __construct(Client $client)
44
    {
45
        parent::__construct($client);
46
    }
47
48
    /**
49
     * @param $userId
50
     * @return $this
51
     */
52
    public function setUserId($userId)
53
    {
54
        $this->userId = $userId;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    private function getUserId()
63
    {
64
        if (is_null($this->userId)) {
65
            throw new \UnexpectedValueException("User id is not set.");
66
        }
67
68
        return $this->userId;
69
    }
70
71
    /**
72
     * @return ResponseInterface
73
     */
74
    public function getUserPreference()
75
    {
76
        $userId = $this->getUserId();
77
78
        return $this->client->get(UserModel::$endpoint . '/' . $userId . '/preferences');
79
    }
80
81
    /**
82
     * @param array $requestOptions
83
     * @return ResponseInterface
84
     */
85
    public function saveUserPreferences(array $requestOptions)
86
    {
87
        $userId = $this->getUserId();
88
89
        return $this->client->put(UserModel::$endpoint . '/' . $userId . '/preferences', $requestOptions);
90
    }
91
92
    /**
93
     * @param array $requestOptions
94
     * @return ResponseInterface
95
     */
96
    public function deleteUserPreferences(array $requestOptions)
97
    {
98
        $userId = $this->getUserId();
99
100
        return $this->client->post(UserModel::$endpoint . '/' . $userId . '/preferences/delete', $requestOptions);
101
    }
102
103
    /**
104
     * @param $category
105
     * @return ResponseInterface
106
     */
107
    public function listUserPreferences($category)
108
    {
109
        $userId = $this->getUserId();
110
111
        return $this->client->get(UserModel::$endpoint . '/' . $userId . '/preferences/' . $category);
112
    }
113
114
    /**
115
     * @param $category
116
     * @param $preferenceName
117
     * @return ResponseInterface
118
     */
119
    public function getSpecificUserPreference($category, $preferenceName)
120
    {
121
        $userId = $this->getUserId();
122
123
        return $this->client->get(UserModel::$endpoint . '/' . $userId . '/preferences/' . $category . '/name/' . $preferenceName);
124
    }
125
126
}