Completed
Pull Request — master (#292)
by Carlos
03:42
created

Menu   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 1
cbo 1
dl 0
loc 78
ccs 17
cts 17
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 4 1
A current() 0 4 1
A add() 0 11 2
A destroy() 0 8 2
A test() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * Menu.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Menu;
22
23
use EasyWeChat\Core\AbstractAPI;
24
25
/**
26
 * Class Menu.
27
 */
28
class Menu extends AbstractAPI
29
{
30
    const API_CREATE = 'https://api.weixin.qq.com/cgi-bin/menu/create';
31
    const API_GET = 'https://api.weixin.qq.com/cgi-bin/menu/get';
32
    const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/menu/delete';
33
    const API_QUERY = 'https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info';
34
    const API_CONDITIONAL_CREATE = 'https://api.weixin.qq.com/cgi-bin/menu/addconditional';
35
    const API_CONDITIONAL_DELETE = 'https://api.weixin.qq.com/cgi-bin/menu/delconditional';
36
    const API_CONDITIONAL_TEST = 'https://api.weixin.qq.com/cgi-bin/menu/trymatch';
37
38
    /**
39
     * Get all menus.
40
     *
41
     * @return \EasyWeChat\Support\Collection
42
     */
43 1
    public function all()
44
    {
45 1
        return $this->parseJSON('get', [self::API_GET]);
46
    }
47
48
    /**
49
     * Get current menus.
50
     *
51
     * @return \EasyWeChat\Support\Collection
52
     */
53 1
    public function current()
54
    {
55 1
        return $this->parseJSON('get', [self::API_QUERY]);
56
    }
57
58
    /**
59
     * Add menu.
60
     *
61
     * @param array $buttons
62
     * @param array $matchRule
63
     *
64
     * @return bool
65
     */
66 1
    public function add(array $buttons, array $matchRule = [])
67
    {
68 1
        if (!empty($matchRule)) {
69 1
            return $this->parseJSON('json', [self::API_CONDITIONAL_CREATE, [
70 1
                'button' => $buttons,
71 1
                'matchrule' => $matchRule,
72 1
            ]]);
73
        }
74
75 1
        return $this->parseJSON('json', [self::API_CREATE, ['button' => $buttons]]);
76
    }
77
78
    /**
79
     * Destroy menu.
80
     *
81
     * @param int $menuId
82
     *
83
     * @return bool
84
     */
85 1
    public function destroy($menuId = null)
86
    {
87 1
        if ($menuId !== null) {
88 1
            return $this->parseJSON('json', [self::API_CONDITIONAL_DELETE, ['menuid' => $menuId]]);
89
        }
90
91 1
        return  $this->parseJSON('get', [self::API_DELETE]);
92
    }
93
94
    /**
95
     * Test conditional menu.
96
     *
97
     * @param string $userId
98
     *
99
     * @return bool
100
     */
101 1
    public function test($userId)
102
    {
103 1
        return $this->parseJSON('json', [self::API_CONDITIONAL_TEST, ['user_id' => $userId]]);
104
    }
105
}
106