Completed
Push — master ( fe47f8...2aa7aa )
by Brian
09:43
created

src/wormling/phparia/Api/Mailboxes.php (1 issue)

Upgrade to new PHP Analysis Engine

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 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
    public function getMailboxes()
39 1
    {
40
        $uri = 'mailboxes';
41 1
        $response = $this->client->getEndpoint()->get($uri);
42 1
43
        $mailboxes = [];
44 1
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $mailbox) {
45 1
            $mailboxes[] = new Mailbox($mailbox);
46 1
        }
47 1
48
        return $mailboxes;
49 1
    }
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)
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
59 2
    {
60
        $uri = "mailboxes/$mailboxName";
61 2
        try {
62
            $response = $this->client->getEndpoint()->get($uri);
63 2
        } catch (RequestException $e) {
64 2
            $this->processRequestException($e);
65 1
        }
66
67
        return new Mailbox(\GuzzleHttp\json_decode($response->getBody()));
68 1
    }
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 1
    {
80
        $uri = "mailboxes/$mailboxName";
81 1
        try {
82
            $this->client->getEndpoint()->put($uri, [
83 1
                'form_params' => [
84 1
                    'newMessages' => $newMessages,
85 1
                    'oldMessages' => $oldMessages,
86 1
                ]
87 1
            ]);
88
        } catch (RequestException $e) {
89
            $this->processRequestException($e);
90 1
        }
91
    }
92
93
    /**
94
     * Destroy a mailbox.
95
     *
96
     * @param string $mailboxName Name of the mailbox
97
     * @throws NotFoundException
98 2
     */
99
    public function deleteMailbox($mailboxName)
100 2
    {
101
        $uri = "mailboxes/$mailboxName";
102 2
        try {
103 2
            $this->client->getEndpoint()->delete($uri);
104 1
        } catch (RequestException $e) {
105
            $this->processRequestException($e);
106 1
        }
107
    }
108
}
109