Passed
Push — master ( 04d469...a7e961 )
by frey
02:53
created

Subscription::set_attributes()   B

Complexity

Conditions 11
Paths 64

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 64
nop 1
dl 0
loc 40
ccs 0
cts 35
cp 0
crap 132
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Freyo\LaravelQueueCMQ\Queue\Driver;
4
5
class Subscription
6
{
7
    private $topic_name;
8
    private $subscription_name;
9
    private $cmq_client;
10
    private $encoding;
11
12
    public function __construct($topic_name, $subscription_name, CMQClient $cmq_client, $encoding = false)
13
    {
14
        $this->topic_name = $topic_name;
15
        $this->subscription_name = $subscription_name;
16
        $this->cmq_client = $cmq_client;
17
        $this->encoding = $encoding;
18
    }
19
20
    public function set_encoding($encoding)
21
    {
22
        $this->encoding = $encoding;
23
    }
24
25
    /*
26
     * create subscription
27
     * @type subscription_meta :SubscriptionMeta
28
     */
29
    public function create($subscription_meta)
30
    {
31
        $params = [
32
            'topicName'           => $this->topic_name,
33
            'subscriptionName'    => $this->subscription_name,
34
            'notifyStrategy'      => $subscription_meta->NotifyStrategy,
35
            'notifyContentFormat' => $subscription_meta->NotifyContentFormat,
36
        ];
37
        if ($subscription_meta->Endpoint != '') {
38
            $params['endpoint'] = $subscription_meta->Endpoint;
39
        }
40
        if ($subscription_meta->Protocol != '') {
41
            $params['protocol'] = $subscription_meta->Protocol;
42
        }
43
44
        if (is_array($subscription_meta->bindingKey) && !empty($subscription_meta->bindingKey)) {
45
            $n = 1;
46
            foreach ($subscription_meta->bindingKey as $tag) {
47
                $key = 'bindingKey.'.$n;
48
                $params[$key] = $tag;
49
                $n += 1;
50
            }
51
        }
52
53
        if (is_array($subscription_meta->FilterTag) && !empty($subscription_meta->FilterTag)) {
54
            $n = 1;
55
            foreach ($subscription_meta->FilterTag as $tag) {
56
                $key = 'filterTag.'.$n;
57
                $params[$key] = $tag;
58
                $n += 1;
59
            }
60
        }
61
        $this->cmq_client->create_subscription($params);
62
    }
63
64
    /*
65
     * delete subscription
66
     */
67
    public function delete()
68
    {
69
        $params = [
70
            'topicName'        => $this->topic_name,
71
            'subscriptionName' => $this->subscription_name,
72
        ];
73
74
        $this->cmq_client->delete_subscription($params);
75
    }
76
77
    /*
78
     * clear subscription tags
79
     */
80
    public function clearFilterTags()
81
    {
82
        $params = [
83
            'topicName'        => $this->topic_name,
84
            'subscriptionName' => $this->subscription_name,
85
        ];
86
87
        $this->cmq_client->clear_filterTags($params);
88
    }
89
90
    /*
91
     * get attributes
92
     *
93
     * @return subscription_meta :SubscriptionMeta
0 ignored issues
show
Bug introduced by
The type Freyo\LaravelQueueCMQ\Qu...river\subscription_meta was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
     */
95
    public function get_attributes()
96
    {
97
        $params = [
98
            'topicName'        => $this->topic_name,
99
            'subscriptionName' => $this->subscription_name,
100
        ];
101
102
        $resp = $this->cmq_client->get_subscription_attributes($params);
103
104
        $subscription_meta = new SubscriptionMeta();
105
        $this->__resp2meta($subscription_meta, $resp);
106
107
        return $subscription_meta;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $subscription_meta returns the type Freyo\LaravelQueueCMQ\Qu...Driver\SubscriptionMeta which is incompatible with the documented return type Freyo\LaravelQueueCMQ\Qu...river\subscription_meta.
Loading history...
108
    }
109
110
    /*
111
     * set attributes
112
     * @type subscription_meta : SubscriptionMeta
113
     *
114
     */
115
116
    protected function __resp2meta($subscription_meta, $resp)
117
    {
118
        if (isset($resp['endpoint'])) {
119
            $subscription_meta->Endpoint = $resp['endpoint'];
120
        }
121
        if (isset($resp['protocol'])) {
122
            $subscription_meta->Protocol = $resp['protocol'];
123
        }
124
        if (isset($resp['notifyStrategy'])) {
125
            $subscription_meta->NotifyStrategy = $resp['notifyStrategy'];
126
        }
127
        if (isset($resp['notifyContentFormat'])) {
128
            $subscription_meta->NotifyContentFormat = $resp['notifyContentFormat'];
129
        }
130
131
        if (isset($resp['bindingKey'])) {
132
            foreach ($resp['bindingKey'] as $tag) {
133
                array_push($subscription_meta->bindingKey, $tag);
134
            }
135
        }
136
137
        if (isset($resp['filterTags'])) {
138
            foreach ($resp['filterTags'] as $tag) {
139
                array_push($subscription_meta->FilterTag, $tag);
140
            }
141
        }
142
    }
143
144
    public function set_attributes($subscription_meta)
145
    {
146
        $params = [
147
            'topicName'        => $this->topic_name,
148
            'subscriptionName' => $this->subscription_name,
149
        ];
150
        if ($subscription_meta->NotifyStrategy != '') {
151
            $params['notifyStrategy'] = $subscription_meta->NotifyStrategy;
152
        }
153
154
        if ($subscription_meta->NotifyContentFormat != '') {
155
            $params['notifyContentFormat'] = $subscription_meta->NotifyContentFormat;
156
        }
157
158
        if ($subscription_meta->Endpoint != '') {
159
            $params['endpoint'] = $subscription_meta->Endpoint;
160
        }
161
        if ($subscription_meta->Protocol != '') {
162
            $params['protocol'] = $subscription_meta->Protocol;
163
        }
164
165
        if (is_array($subscription_meta->bindingKey) && !empty($subscription_meta->bindingKey)) {
166
            $n = 1;
167
            foreach ($subscription_meta->bindingKey as $tag) {
168
                $key = 'bindingKey.'.$n;
169
                $params[$key] = $tag;
170
                $n += 1;
171
            }
172
        }
173
174
        if (is_array($subscription_meta->FilterTag) && !empty($subscription_meta->FilterTag)) {
175
            $n = 1;
176
            foreach ($subscription_meta->FilterTag as $tag) {
177
                $key = 'filterTag.'.$n;
178
                $params[$key] = $tag;
179
                $n += 1;
180
            }
181
        }
182
183
        $this->cmq_client->set_subscription_attributes($params);
184
    }
185
}
186