These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 Pest_NotFound; |
||
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 | /** |
||
35 | * List all mailboxes. |
||
36 | * |
||
37 | * @return Mailbox[] |
||
38 | */ |
||
39 | 1 | public function getMailboxes() |
|
40 | { |
||
41 | 1 | $uri = '/mailboxes'; |
|
42 | 1 | $response = $this->client->getEndpoint()->get($uri); |
|
43 | |||
44 | 1 | $mailboxes = []; |
|
45 | 1 | foreach ((array)$response as $mailbox) { |
|
46 | 1 | $mailboxes[] = new Mailbox($mailbox); |
|
47 | 1 | } |
|
48 | |||
49 | 1 | return $mailboxes; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Retrieve the current state of a mailbox. |
||
54 | * |
||
55 | * @param string $mailboxName Name of the mailbox |
||
56 | * @return Mailbox |
||
57 | * @throws NotFoundException |
||
58 | */ |
||
59 | 2 | public function getMailbox($mailboxName) |
|
60 | { |
||
61 | 2 | $uri = "/mailboxes/$mailboxName"; |
|
62 | try { |
||
63 | 2 | $response = $this->client->getEndpoint()->get($uri); |
|
64 | 2 | } catch (Pest_NotFound $e) { // Mailbox not found |
|
65 | 1 | throw new NotFoundException($e); |
|
66 | } |
||
67 | |||
68 | 1 | return new Mailbox($response); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Change the state of a mailbox. (Note - implicitly creates the mailbox). |
||
73 | * |
||
74 | * @param string $mailboxName Name of the mailbox |
||
75 | * @param int $oldMessages (required) Count of old messages in the mailbox |
||
76 | * @param int $newMessages (required) Count of new messages in the mailbox |
||
77 | * @throws NotFoundException |
||
78 | */ |
||
79 | 1 | public function updateMailbox($mailboxName, $oldMessages, $newMessages) |
|
80 | { |
||
81 | 1 | $uri = "/mailboxes/$mailboxName"; |
|
82 | try { |
||
83 | 1 | $this->client->getEndpoint()->put($uri, array( |
|
84 | 1 | 'newMessages' => $newMessages, |
|
85 | 1 | 'oldMessages' => $oldMessages, |
|
86 | 1 | )); |
|
87 | 1 | } catch (Pest_NotFound $e) { // Mailbox not found |
|
88 | throw new NotFoundException($e); |
||
89 | } |
||
90 | 1 | } |
|
91 | |||
92 | /** |
||
93 | * Destroy a mailbox. |
||
94 | * |
||
95 | * @param string $mailboxName Name of the mailbox |
||
96 | * @throws NotFoundException |
||
97 | */ |
||
98 | 2 | View Code Duplication | public function deleteMailbox($mailboxName) |
0 ignored issues
–
show
|
|||
99 | { |
||
100 | 2 | $uri = "/mailboxes/$mailboxName"; |
|
101 | try { |
||
102 | 2 | $this->client->getEndpoint()->delete($uri); |
|
103 | 2 | } catch (Pest_NotFound $e) { // Mailbox not found |
|
104 | 1 | throw new NotFoundException($e); |
|
105 | } |
||
106 | 1 | } |
|
107 | |||
108 | } |
||
109 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.