Completed
Push — master ( 017947...8f5b55 )
by frey
13s
created

Authorization::getAuthorizerCorpId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Suite;
4
5
use Doctrine\Common\Cache\Cache;
6
use EntWeChat\Core\Exception;
7
use EntWeChat\Suite\Api\BaseApi;
8
9
class Authorization
10
{
11
    const CACHE_KEY_ACCESS_TOKEN = 'EntWeChat.suite.authorizer_access_token';
12
    const CACHE_KEY_PERMANENT_CODE = 'EntWeChat.suite.authorizer_permanent_code';
13
14
    /**
15
     * Cache.
16
     *
17
     * @var \Doctrine\Common\Cache\Cache
18
     */
19
    protected $cache;
20
21
    /**
22
     * Base API.
23
     *
24
     * @var \EntWeChat\Suite\Api\BaseApi
25
     */
26
    protected $api;
27
28
    /**
29
     * Suite Id.
30
     *
31
     * @var string
32
     */
33
    protected $suiteId;
34
35
    /**
36
     * Authorizer Corp Id.
37
     *
38
     * @var string
39
     */
40
    protected $authorizerCorpId;
41
42
    /**
43
     * Authorization Constructor.
44
     *
45
     * @param \EntWeChat\Suite\Api\BaseApi $api
46
     * @param string                       $suiteId
47
     * @param \Doctrine\Common\Cache\Cache $cache
48
     */
49
    public function __construct(BaseApi $api, $suiteId, Cache $cache)
50
    {
51
        $this->api = $api;
52
        $this->suiteId = $suiteId;
53
        $this->cache = $cache;
54
    }
55
56
    /**
57
     * Gets the base api.
58
     *
59
     * @return \EntWeChat\Suite\Api\BaseApi
60
     */
61
    public function getApi()
62
    {
63
        return $this->api;
64
    }
65
66
    /**
67
     * Saves the authorizer access token in cache.
68
     *
69
     * @param string $token
70
     *
71
     * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise
72
     */
73
    public function setAuthorizerAccessToken($token, $expires = 7200)
74
    {
75
        return $this->cache->save($this->getAuthorizerAccessTokenKey(), $token, $expires);
76
    }
77
78
    /**
79
     * Gets the authorizer access token cache key.
80
     *
81
     * @return string
82
     */
83
    public function getAuthorizerAccessTokenKey()
84
    {
85
        return self::CACHE_KEY_ACCESS_TOKEN.$this->suiteId.$this->getAuthorizerCorpId();
86
    }
87
88
    /**
89
     * Gets the authorizer corp id, or throws if not found.
90
     *
91
     * @throws \EntWeChat\Core\Exception
92
     *
93
     * @return string
94
     */
95
    public function getAuthorizerCorpId()
96
    {
97
        if (!$this->authorizerCorpId) {
98
            throw new Exception(
99
                'Authorizer Corp Id is not present, you may not make the authorization yet.'
100
            );
101
        }
102
103
        return $this->authorizerCorpId;
104
    }
105
106
    /**
107
     * Sets the authorizer corp id.
108
     *
109
     * @param string $authorizerCorpId
110
     *
111
     * @return $this
112
     */
113
    public function setAuthorizerCorpId($authorizerCorpId)
114
    {
115
        $this->authorizerCorpId = $authorizerCorpId;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Gets the authorizer access token.
122
     *
123
     * @return string
124
     */
125
    public function getAuthorizerAccessToken()
126
    {
127
        return $this->cache->fetch($this->getAuthorizerAccessTokenKey());
128
    }
129
130
    /**
131
     * Saves the authorizer refresh token in cache.
132
     *
133
     * @param string $permanentCode
134
     *
135
     * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise
136
     */
137
    public function setAuthorizerPermanentCode($permanentCode)
138
    {
139
        return $this->cache->save($this->getAuthorizerPermanentCodeKey(), $permanentCode);
140
    }
141
142
    /**
143
     * Gets the authorizer refresh token cache key.
144
     *
145
     * @return string
146
     */
147
    public function getAuthorizerPermanentCodeKey()
148
    {
149
        return self::CACHE_KEY_PERMANENT_CODE.$this->suiteId.$this->getAuthorizerCorpId();
150
    }
151
152
    /**
153
     * Gets the authorizer refresh token.
154
     *
155
     * @throws \EntWeChat\Core\Exception when refresh token is not present
156
     *
157
     * @return string
158
     */
159
    public function getAuthorizerPermanentCode()
160
    {
161
        if ($permanentCode = $this->cache->fetch($this->getAuthorizerPermanentCodeKey())) {
162
            return $permanentCode;
163
        }
164
165
        throw new Exception(
166
            'Authorizer Permanent Code is not present, you may not make the authorization yet.'
167
        );
168
    }
169
170
    /**
171
     * Removes the authorizer access token from cache.
172
     *
173
     * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
174
     *              Deleting a non-existing entry is considered successful
175
     */
176
    public function removeAuthorizerAccessToken()
177
    {
178
        return $this->cache->delete($this->getAuthorizerAccessTokenKey());
179
    }
180
181
    /**
182
     * Removes the authorizer refresh token from cache.
183
     *
184
     * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
185
     *              Deleting a non-existing entry is considered successful
186
     */
187
    public function removeAuthorizerPermanentCode()
188
    {
189
        return $this->cache->delete($this->getAuthorizerPermanentCodeKey());
190
    }
191
}
192