Completed
Pull Request — master (#249)
by David
18:43 queued 08:42
created

src/Mailgun/Api/Routes.php (5 issues)

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 (C) 2013-2016 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Api;
11
12
use Mailgun\Assert;
13
use Mailgun\Resource\Api\Routes\Dto\RouteDto;
14
use Mailgun\Resource\Api\Routes\Response\CreateResponse;
15
use Mailgun\Resource\Api\Routes\Response\DeleteResponse;
16
use Mailgun\Resource\Api\Routes\Response\IndexResponse;
17
use Mailgun\Resource\Api\Routes\Response\ShowResponse;
18
use Mailgun\Resource\Api\Routes\Response\UpdateResponse;
19
20
/**
21
 * {@link https://documentation.mailgun.com/api-routes.html}.
22
 *
23
 * @author David Garcia <[email protected]>
24
 */
25
class Routes extends HttpApi
26
{
27
    /**
28
     * Fetches the list of Routes. Note that Routes are defined globally,
29
     * per account, not per domain as most of other API calls.
30
     *
31
     * @param int $limit Maximum number of records to return. (100 by default)
32
     * @param int $skip  Number of records to skip. (0 by default)
33
     *
34
     * @return IndexResponse
35
     */
36 View Code Duplication
    public function index($limit = 100, $skip = 0)
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...
37
    {
38
        Assert::integer($limit);
39
        Assert::integer($skip);
40
        Assert::greaterThan($limit, 0);
41
        Assert::greaterThanEq($skip, 0);
42
43
        $params = [
44
            'limit' => $limit,
45
            'skip' => $skip,
46
        ];
47
48
        $response = $this->httpGet('/v3/routes', $params);
49
50
        return $this->safeDeserialize($response, IndexResponse::class);
51
    }
52
53
    /**
54
     * Returns a single Route object based on its ID.
55
     *
56
     * @param string $routeId Route ID returned by the Routes::index() method
57
     *
58
     * @return RouteDto
59
     */
60
    public function show($routeId)
61
    {
62
        Assert::stringNotEmpty($routeId);
63
64
        $response = $this->httpGet(sprintf('/v3/routes/%s', $routeId));
65
66
        return $this->safeDeserialize($response, ShowResponse::class);
67
    }
68
69
    /**
70
     * Creates a new Route.
71
     *
72
     * @param string $expression  A filter expression like "match_recipient('.*@gmail.com')"
73
     * @param array  $actions     Route action. This action is executed when the expression evaluates to True. Example: "forward('[email protected]')"
74
     * @param string $description An arbitrary string
75
     * @param int    $priority    Integer: smaller number indicates higher priority. Higher priority routes are handled first. Defaults to 0.
76
     *
77
     * @return CreateResponse
78
     */
79 View Code Duplication
    public function create($expression, array $actions, $description, $priority = 0)
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...
80
    {
81
        Assert::string($expression);
82
        Assert::isArray($actions);
83
        Assert::string($description);
84
        Assert::integer($priority);
85
86
        $params = [
87
            'priority' => $priority,
88
            'expression' => $expression,
89
            'action' => $actions,
90
            'description' => $description,
91
        ];
92
93
        $response = $this->httpPost('/v3/routes', $params);
94
95
        return $this->safeDeserialize($response, CreateResponse::class);
96
    }
97
98
    /**
99
     * Updates a given Route by ID. All parameters are optional.
100
     * This API call only updates the specified fields leaving others unchanged.
101
     *
102
     * @param string      $routeId     Route ID returned by the Routes::index() method
103
     * @param string|null $expression  A filter expression like "match_recipient('.*@gmail.com')"
104
     * @param array|null  $actions     Route action. This action is executed when the expression evaluates to True. Example: "forward('[email protected]')"
105
     * @param string|null $description An arbitrary string
106
     * @param int|null    $priority    Integer: smaller number indicates higher priority. Higher priority routes are handled first. Defaults to 0.
107
     *
108
     * @return UpdateResponse
109
     */
110
    public function update($routeId, $expression = null, array $actions = null, $description = null, $priority = null)
111
    {
112
        Assert::stringNotEmpty($routeId);
113
114
        $params = [];
115
116 View Code Duplication
        if (is_string($expression) && 0 < strlen($expression)) {
0 ignored issues
show
This code seems to be duplicated across 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...
117
            $params['expression'] = trim($expression);
118
        }
119
120
        if (is_array($actions)) {
121
            foreach ($actions as $action) {
122
                Assert::stringNotEmpty($action);
123
124
                $params['action'] = isset($params['action']) ? $params['action'] : [];
125
                $params['action'][] = $action;
126
            }
127
        }
128
129 View Code Duplication
        if (is_string($description) && 0 < strlen($description)) {
0 ignored issues
show
This code seems to be duplicated across 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...
130
            $params['description'] = trim($description);
131
        }
132
133
        if (is_int($priority)) {
134
            $params['priority'] = $priority;
135
        }
136
137
        $response = $this->httpPut(sprintf('/v3/routes/%s', $routeId), $params);
138
139
        return $this->safeDeserialize($response, UpdateResponse::class);
140
    }
141
142
    /**
143
     * Deletes a Route based on the ID.
144
     *
145
     * @param string $routeId Route ID returned by the Routes::index() method
146
     *
147
     * @return DeleteResponse
148
     */
149 View Code Duplication
    public function delete($routeId)
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...
150
    {
151
        Assert::stringNotEmpty($routeId);
152
153
        $response = $this->httpDelete(sprintf('/v3/routes/%s', $routeId));
154
155
        return $this->safeDeserialize($response, DeleteResponse::class);
156
    }
157
}
158