Completed
Push — master ( a3c7a7...bd72ff )
by Carlos
02:47
created

Group::updateProduct()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 6.7273
cc 7
eloc 10
nc 4
nop 2
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
 * @link      https://github.com/a939638621
13
 */
14
15
namespace Overtrue\Wechat\Shop;
16
17
18
use Overtrue\Wechat\Shop\Foundation\Base;
19
use Overtrue\Wechat\Shop\Foundation\Group as GroupInterface;
20
use Overtrue\Wechat\Shop\Foundation\ShopsException;
21
22
/**
23
 * 分组管理
24
 *
25
 * Class Group
26
 * @package Shop
27
 */
28
class Group extends Base implements GroupInterface
29
{
30
31
    const API_ADD = 'https://api.weixin.qq.com/merchant/group/add';
32
    const API_DELETE = 'https://api.weixin.qq.com/merchant/group/del';
33
    const API_UPDATE_ATTRIBUTE = 'https://api.weixin.qq.com/merchant/group/propertymod';
34
    const API_UPDATE_PRODUCT = 'https://api.weixin.qq.com/merchant/group/productmod';
35
    const API_LISTS = 'https://api.weixin.qq.com/merchant/group/getall';
36
    const API_GET_BY_ID = 'https://api.weixin.qq.com/merchant/group/getbyid';
37
38
    /**
39
     * 添加分组
40
     *
41
     * @param $groupName
42
     * @param array $productList
43
     * @return mixed
44
     * @throws
45
     */
46
    public function add($groupName, array $productList)
47
    {
48
        foreach (array_keys($productList) as $v) {
49
            if (!is_numeric($v)) {
50
                throw new ShopsException('请插入索引数组');
51
            }
52
        }
53
54
        $this->response = $this->http->jsonPost(self::API_ADD,array(
55
            'group_detail' => array(
56
                'group_name' => $groupName,
57
                'product_list' =>$productList
58
            )
59
        ));
60
61
        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...
62
    }
63
64
    /**
65
     * 删除分组
66
     *
67
     * @param $groupId
68
     * @return bool
69
     * @throws ShopsException
70
     */
71
    public function delete($groupId)
72
    {
73
        $this->response = $this->http->jsonPost(self::API_DELETE,array(
74
            'group_id' => $groupId
75
        ));
76
77
        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 77 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Group::delete of type boolean.
Loading history...
78
    }
79
80
    /**
81
     * 修改分组属性
82
     *
83
     * @param $groupId
84
     * @param $groupName
85
     * @return bool
86
     * @throws ShopsException
87
     */
88
    public function updateAttribute($groupId, $groupName)
89
    {
90
        $this->response = $this->http->jsonPost(self::API_UPDATE_ATTRIBUTE,array(
91
            'group_id' => $groupId,
92
            'group_name' => $groupName
93
        ));
94
95
        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 95 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Fou...\Group::updateAttribute of type boolean.
Loading history...
96
    }
97
98
    /**
99
     * 修改分组商品
100
     *
101
     * @param $groupId
102
     * @param array $product
103
     * @return bool
104
     * @throws ShopsException
105
     */
106
    public function updateProduct($groupId, array $product)
107
    {
108
        foreach ($product as $v) {
109
110
            $keys = array_keys($v);
111
112
            if (count($keys) == 2) {
113
                if (!( $keys[0] == 'product_id' && $keys[1] == 'mod_action' )) {
114
                    $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...
115
                }
116
117
            }
118
119
        }
120
121
        $this->response = $this->http->jsonPost(self::API_UPDATE_PRODUCT,array(
122
            'group_id' => $groupId,
123
            'product' => isset($data) && is_array($data) ? $data : $product
124
        ));
125
126
        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 126 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Fou...on\Group::updateProduct of type boolean.
Loading history...
127
128
    }
129
130
    /**
131
     * 获得全部商品
132
     *
133
     * @return array
134
     * @throws ShopsException
135
     */
136
    public function lists()
137
    {
138
        $this->response = $this->http->get(self::API_LISTS);
139
140
        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 140 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Group::lists of type array.
Loading history...
141
    }
142
143
    /**
144
     * 根据分组ID获取分组信息
145
     *
146
     * @param $groupId
147
     * @return array
148
     * @throws ShopsException
149
     */
150
    public function getById($groupId)
151
    {
152
        $this->response = $this->http->jsonPost(self::API_GET_BY_ID,array(
153
            'group_id' => $groupId
154
        ));
155
156
        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 156 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Group::getById of type array.
Loading history...
157
    }
158
159
}