Passed
Push — main ( 181dde...f53085 )
by huang
04:02
created

AodManager::getAlbumsGetBatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Xmly\API;
4
5
use Xmly\Auth;
6
use Xmly\Config;
7
use Xmly\Http\Client;
8
use Xmly\Http\Error;
9
use Xmly\Util;
10
11
final class AodManager
12
{
13
    private $auth;
14
    private $config;
15
16 6
    public function __construct(Auth $auth, Config $config = null)
17
    {
18 6
        $this->auth = $auth;
19 6
        if ($config == null) {
20 6
            $this->config = new Config();
21
        } else {
22 6
            $this->config = $config;
23
        }
24 6
    }
25
26
    /**
27
     * 点播-免费内容-获取分类列表
28
     *
29
     * @param  array  $body                请求参数
30
     * @param  string $serverAuthStaticKey
31
     * @return array
32
     *
33
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=10&articleId=6#%E5%88%86%E7%B1%BB%E5%88%97%E8%A1%A8
34
     */
35 1
    public function getCategoriesList($body, $serverAuthStaticKey = null)
36
    {
37 1
        $scheme = "http://";
38 1
        if ($this->config->useHTTPS === true) {
39 1
            $scheme = "https://";
40
        }
41 1
        $sigURL = $this->auth->signatureURL($body, $serverAuthStaticKey);
42 1
        $url = $scheme . Config::API_HOST . '/categories/list?' . $sigURL;
43 1
        return $this->get($url);
44
    }
45
46
    /**
47
     * 点播-免费内容-获取标签列表
48
     *
49
     * @param  array  $body                请求参数
50
     * @param  string $serverAuthStaticKey
51
     * @return array
52
     *
53
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=10&articleId=6#%E6%A0%87%E7%AD%BE%E5%88%97%E8%A1%A8
54
     */
55 1
    public function getTagsList($body, $serverAuthStaticKey = null)
56
    {
57 1
        $scheme = "http://";
58 1
        if ($this->config->useHTTPS === true) {
59 1
            $scheme = "https://";
60
        }
61 1
        $sigURL = $this->auth->signatureURL($body, $serverAuthStaticKey);
62 1
        $url = $scheme . Config::API_HOST . '/v2/tags/list?' . $sigURL;
63 1
        return $this->get($url);
64
    }
65
66
    /**
67
     * 点播-免费内容-获取专辑列表
68
     *
69
     * @param  array  $body                请求参数
70
     * @param  string $serverAuthStaticKey
71
     * @return array
72
     *
73
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=10&articleId=6#%E4%B8%93%E8%BE%91%E5%88%97%E8%A1%A8
74
     */
75 1
    public function getAlbumsList($body, $serverAuthStaticKey = null)
76
    {
77 1
        $scheme = "http://";
78 1
        if ($this->config->useHTTPS === true) {
79 1
            $scheme = "https://";
80
        }
81 1
        $sigURL = $this->auth->signatureURL($body, $serverAuthStaticKey);
82 1
        $url = $scheme . Config::API_HOST . '/v2/albums/list?' . $sigURL;
83 1
        return $this->get($url);
84
    }
85
86
    /**
87
     * 点播-免费内容-获取专辑下的声音列表
88
     *
89
     * @param  array  $body                请求参数
90
     * @param  string $serverAuthStaticKey
91
     * @return array
92
     *
93
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=10&articleId=6#%E8%8E%B7%E5%8F%96%E4%B8%93%E8%BE%91%E4%B8%8B%E7%9A%84%E5%A3%B0%E9%9F%B3%E5%88%97%E8%A1%A8
94
     */
95 1
    public function getAlbumsBrowse($body, $serverAuthStaticKey = null)
96
    {
97 1
        $scheme = "http://";
98 1
        if ($this->config->useHTTPS === true) {
99 1
            $scheme = "https://";
100
        }
101 1
        $sigURL = $this->auth->signatureURL($body, $serverAuthStaticKey);
102 1
        $url = $scheme . Config::API_HOST . '/albums/browse?' . $sigURL;
103 1
        return $this->get($url);
104
    }
105
106
    /**
107
     * 点播-免费内容-批量获取专辑信息
108
     *
109
     * @param  array  $body                请求参数
110
     * @param  string $serverAuthStaticKey
111
     * @return array
112
     *
113
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=10&articleId=6#%E6%89%B9%E9%87%8F%E8%8E%B7%E5%8F%96%E4%B8%93%E8%BE%91%E4%BF%A1%E6%81%AF
114
     */
115 1
    public function getAlbumsGetBatch($body, $serverAuthStaticKey = null)
116
    {
117 1
        $scheme = "http://";
118 1
        if ($this->config->useHTTPS === true) {
119 1
            $scheme = "https://";
120
        }
121 1
        $sigURL = $this->auth->signatureURL($body, $serverAuthStaticKey);
122 1
        $url = $scheme . Config::API_HOST . '/albums/get_batch?' . $sigURL;
123 1
        return $this->get($url);
124
    }
125
126
    /**
127
     * 点播-主播-获取主播分类列表
128
     *
129
     * @param  array  $body                请求参数
130
     * @param  string $serverAuthStaticKey
131
     * @return array
132
     *
133
     * @link https://open.ximalaya.com/doc/detailApi?categoryId=10&articleId=61#%E8%8E%B7%E5%8F%96%E4%B8%BB%E6%92%AD%E5%88%86%E7%B1%BB%E5%88%97%E8%A1%A8
134
     */
135 1
    public function getAnnouncersCategories($body, $serverAuthStaticKey = null)
136
    {
137 1
        $scheme = "http://";
138 1
        if ($this->config->useHTTPS === true) {
139 1
            $scheme = "https://";
140
        }
141
142 1
        $sigURL = $this->auth->signatureURL($body, $serverAuthStaticKey);
143 1
        $url = $scheme . Config::API_HOST . '/announcers/categories?' . $sigURL;
144 1
        return $this->get($url);
145
    }
146
147
    /**
148
     * Get 请求方法
149
     *
150
     * @param  string $url 完整的带 sig 的请求 url
151
     * @return array
152
     */
153 6
    private function get($url)
154
    {
155 6
        $headers = array();
156 6
        $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
157 6
        $ret = Client::get($url, $headers);
158 6
        if (!$ret->ok()) {
159 1
            Util::writeErrLog($url, $ret->body, $ret->statusCode, $ret->duration, $ret->error);
160 1
            return array(null, new Error($url, $ret));
161
        }
162 6
        if ($this->config->enableLogs === true) {
163 6
            Util::writeInfoLog($url, $ret->body, $ret->statusCode, $ret->duration);
164
        }
165 6
        return array($ret->json(), null);
166
    }
167
}
168