Mailboxes   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 29.49 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 23
loc 78
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMailboxes() 12 12 2
A getMailbox() 11 11 2
A updateMailbox() 0 14 2
A deleteMailbox() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * Copyright 2014 Brian Smith <[email protected]>.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *      http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace phparia\Api;
20
21
use GuzzleHttp\Exception\RequestException;
22
use phparia\Client\AriClientAware;
23
use phparia\Exception\NotFoundException;
24
use phparia\Resources\Mailbox;
25
26
/**
27
 * Mailboxes API
28
 *
29
 * @author Brian Smith <[email protected]>
30
 */
31
class Mailboxes extends AriClientAware
32
{
33
    /**
34
     * List all mailboxes.
35
     *
36
     * @return Mailbox[]
37
     */
38 View Code Duplication
    public function getMailboxes()
39
    {
40
        $uri = 'mailboxes';
41
        $response = $this->client->getEndpoint()->get($uri);
42
43
        $mailboxes = [];
44
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $mailbox) {
45
            $mailboxes[] = new Mailbox($mailbox);
46
        }
47
48
        return $mailboxes;
49
    }
50
51
    /**
52
     * Retrieve the current state of a mailbox.
53
     *
54
     * @param string $mailboxName Name of the mailbox
55
     * @return Mailbox
56
     * @throws NotFoundException
57
     */
58 View Code Duplication
    public function getMailbox($mailboxName)
59
    {
60
        $uri = "mailboxes/$mailboxName";
61
        try {
62
            $response = $this->client->getEndpoint()->get($uri);
63
        } catch (RequestException $e) {
64
            $this->processRequestException($e);
65
        }
66
67
        return new Mailbox(\GuzzleHttp\json_decode($response->getBody()));
68
    }
69
70
    /**
71
     * Change the state of a mailbox. (Note - implicitly creates the mailbox).
72
     *
73
     * @param string $mailboxName Name of the mailbox
74
     * @param int $oldMessages (required) Count of old messages in the mailbox
75
     * @param int $newMessages (required) Count of new messages in the mailbox
76
     * @throws NotFoundException
77
     */
78
    public function updateMailbox($mailboxName, $oldMessages, $newMessages)
79
    {
80
        $uri = "mailboxes/$mailboxName";
81
        try {
82
            $this->client->getEndpoint()->put($uri, [
83
                'form_params' => [
84
                    'newMessages' => $newMessages,
85
                    'oldMessages' => $oldMessages,
86
                ]
87
            ]);
88
        } catch (RequestException $e) {
89
            $this->processRequestException($e);
90
        }
91
    }
92
93
    /**
94
     * Destroy a mailbox.
95
     *
96
     * @param string $mailboxName Name of the mailbox
97
     * @throws NotFoundException
98
     */
99
    public function deleteMailbox($mailboxName)
100
    {
101
        $uri = "mailboxes/$mailboxName";
102
        try {
103
            $this->client->getEndpoint()->delete($uri);
104
        } catch (RequestException $e) {
105
            $this->processRequestException($e);
106
        }
107
    }
108
}
109