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