Completed
Pull Request — master (#200)
by Carlos
04:52 queued 02:20
created

Group::lists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Group.php
4
 *
5
 * Part of Overtrue\Wechat.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    a939638621 <[email protected]>
11
 * @copyright 2015 a939638621 <[email protected]>
12
 *
13
 * @link      https://github.com/a939638621
14
 */
15
namespace Overtrue\Wechat\Shop;
16
17
use Overtrue\Wechat\Shop\Foundation\Base;
18
use Overtrue\Wechat\Shop\Foundation\Group as GroupInterface;
19
use Overtrue\Wechat\Shop\Foundation\ShopsException;
20
21
/**
22
 * 分组管理
23
 *
24
 * Class Group
25
 */
26
class Group extends Base implements GroupInterface
27
{
28
    const API_ADD              = 'https://api.weixin.qq.com/merchant/group/add';
29
    const API_DELETE           = 'https://api.weixin.qq.com/merchant/group/del';
30
    const API_UPDATE_ATTRIBUTE = 'https://api.weixin.qq.com/merchant/group/propertymod';
31
    const API_UPDATE_PRODUCT   = 'https://api.weixin.qq.com/merchant/group/productmod';
32
    const API_LISTS            = 'https://api.weixin.qq.com/merchant/group/getall';
33
    const API_GET_BY_ID        = 'https://api.weixin.qq.com/merchant/group/getbyid';
34
35
    /**
36
     * 添加分组
37
     *
38
     * @param $groupName
39
     * @param array $productList
40
     *
41
     * @return mixed
42
     *
43
     * @throws
44
     */
45
    public function add($groupName, array $productList)
46
    {
47
        foreach (array_keys($productList) as $v) {
48
            if (!is_numeric($v)) {
49
                throw new ShopsException('请插入索引数组');
50
            }
51
        }
52
53
        $this->response = $this->http->jsonPost(self::API_ADD, array(
54
            'group_detail' => array(
55
                'group_name' => $groupName,
56
                'product_list' => $productList,
57
            ),
58
        ));
59
60
        return $this->getResponse();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getResponse(); (boolean|array) is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Group::add of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
61
    }
62
63
    /**
64
     * 删除分组
65
     *
66
     * @param $groupId
67
     *
68
     * @return bool
69
     *
70
     * @throws ShopsException
71
     */
72
    public function delete($groupId)
73
    {
74
        $this->response = $this->http->jsonPost(self::API_DELETE, array(
75
            'group_id' => $groupId,
76
        ));
77
78
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type array to the return on line 78 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Group::delete of type boolean.
Loading history...
79
    }
80
81
    /**
82
     * 修改分组属性
83
     *
84
     * @param $groupId
85
     * @param $groupName
86
     *
87
     * @return bool
88
     *
89
     * @throws ShopsException
90
     */
91
    public function updateAttribute($groupId, $groupName)
92
    {
93
        $this->response = $this->http->jsonPost(self::API_UPDATE_ATTRIBUTE, array(
94
            'group_id' => $groupId,
95
            'group_name' => $groupName,
96
        ));
97
98
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type array to the return on line 98 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Fou...\Group::updateAttribute of type boolean.
Loading history...
99
    }
100
101
    /**
102
     * 修改分组商品
103
     *
104
     * @param $groupId
105
     * @param array $product
106
     *
107
     * @return bool
108
     *
109
     * @throws ShopsException
110
     */
111
    public function updateProduct($groupId, array $product)
112
    {
113
        foreach ($product as $v) {
114
            $keys = array_keys($v);
115
116
            if (count($keys) === 2) {
117
                if (!($keys[0] === 'product_id' && $keys[1] === 'mod_action')) {
118
                    $data[] = array('product_id' => $v[$keys[0]], 'mod_action' => $v[$keys[1]]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
119
                }
120
            }
121
        }
122
123
        $this->response = $this->http->jsonPost(self::API_UPDATE_PRODUCT, array(
124
            'group_id' => $groupId,
125
            'product' => isset($data) && is_array($data) ? $data : $product,
126
        ));
127
128
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type array to the return on line 128 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Fou...on\Group::updateProduct of type boolean.
Loading history...
129
    }
130
131
    /**
132
     * 获得全部商品
133
     *
134
     * @return array
135
     *
136
     * @throws ShopsException
137
     */
138
    public function lists()
139
    {
140
        $this->response = $this->http->get(self::API_LISTS);
141
142
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type boolean to the return on line 142 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Group::lists of type array.
Loading history...
143
    }
144
145
    /**
146
     * 根据分组ID获取分组信息
147
     *
148
     * @param $groupId
149
     *
150
     * @return array
151
     *
152
     * @throws ShopsException
153
     */
154
    public function getById($groupId)
155
    {
156
        $this->response = $this->http->jsonPost(self::API_GET_BY_ID, array(
157
            'group_id' => $groupId,
158
        ));
159
160
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type boolean to the return on line 160 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Group::getById of type array.
Loading history...
161
    }
162
}
163