Completed
Push — master ( ef3dc6...e28065 )
by David
03:47
created

src/Mailgun/Api/Route.php (2 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 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\Model\Route\Response\CreateResponse;
14
use Mailgun\Model\Route\Response\DeleteResponse;
15
use Mailgun\Model\Route\Response\IndexResponse;
16
use Mailgun\Model\Route\Response\ShowResponse;
17
use Mailgun\Model\Route\Response\UpdateResponse;
18
19
/**
20
 * {@link https://documentation.mailgun.com/api-routes.html}.
21
 *
22
 * @author David Garcia <[email protected]>
23
 */
24
class Route extends HttpApi
25
{
26
    /**
27
     * Fetches the list of Routes.
28
     *
29
     * @param int $limit Maximum number of records to return. (100 by default)
30
     * @param int $skip  Number of records to skip. (0 by default)
31
     *
32
     * @return IndexResponse
33
     */
34 1 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...
35
    {
36 1
        Assert::integer($limit);
37 1
        Assert::integer($skip);
38 1
        Assert::greaterThan($limit, 0);
39 1
        Assert::greaterThanEq($skip, 0);
40
41
        $params = [
42 1
            'limit' => $limit,
43 1
            'skip' => $skip,
44 1
        ];
45
46 1
        $response = $this->httpGet('/v3/routes', $params);
47
48 1
        return $this->hydrateResponse($response, IndexResponse::class);
49
    }
50
51
    /**
52
     * Returns a single Route object based on its ID.
53
     *
54
     * @param string $routeId Route ID returned by the Routes::index() method
55
     *
56
     * @return ShowResponse
57
     */
58 1
    public function show($routeId)
59
    {
60 1
        Assert::stringNotEmpty($routeId);
61
62 1
        $response = $this->httpGet(sprintf('/v3/routes/%s', $routeId));
63
64 1
        return $this->hydrateResponse($response, ShowResponse::class);
65
    }
66
67
    /**
68
     * Creates a new Route.
69
     *
70
     * @param string $expression  A filter expression like "match_recipient('.*@gmail.com')"
71
     * @param array  $actions     Route action. This action is executed when the expression evaluates to True. Example: "forward('[email protected]')"
72
     * @param string $description An arbitrary string
73
     * @param int    $priority    Integer: smaller number indicates higher priority. Higher priority routes are handled first. Defaults to 0.
74
     *
75
     * @return CreateResponse
76
     */
77 3 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...
78
    {
79 3
        Assert::string($expression);
80 3
        Assert::isArray($actions);
81 3
        Assert::string($description);
82 3
        Assert::integer($priority);
83
84
        $params = [
85 3
            'priority' => $priority,
86 3
            'expression' => $expression,
87 3
            'action' => $actions,
88 3
            'description' => $description,
89 3
        ];
90
91 3
        $response = $this->httpPost('/v3/routes', $params);
92
93 3
        return $this->hydrateResponse($response, CreateResponse::class);
94
    }
95
96
    /**
97
     * Updates a given Route by ID. All parameters are optional.
98
     * This API call only updates the specified fields leaving others unchanged.
99
     *
100
     * @param string      $routeId     Route ID returned by the Routes::index() method
101
     * @param string|null $expression  A filter expression like "match_recipient('.*@gmail.com')"
102
     * @param array|null  $actions     Route action. This action is executed when the expression evaluates to True. Example: "forward('[email protected]')"
103
     * @param string|null $description An arbitrary string
104
     * @param int|null    $priority    Integer: smaller number indicates higher priority. Higher priority routes are handled first. Defaults to 0.
105
     *
106
     * @return UpdateResponse
107
     */
108 1
    public function update($routeId, $expression = null, array $actions = [], $description = null, $priority = null)
109
    {
110 1
        Assert::stringNotEmpty($routeId);
111 1
        Assert::nullOrString($expression);
112 1
        Assert::isArray($actions);
113 1
        Assert::nullOrString($description);
114 1
        Assert::nullOrInteger($priority);
115
116 1
        $params = [];
117
118 1
        if (!empty($expression)) {
119 1
            $params['expression'] = trim($expression);
120 1
        }
121
122 1
        foreach ($actions as $action) {
123 1
            Assert::stringNotEmpty($action);
124
125 1
            $params['action'] = isset($params['action']) ? $params['action'] : [];
126 1
            $params['action'][] = $action;
127 1
        }
128
129 1
        if (!empty($description)) {
130 1
            $params['description'] = trim($description);
131 1
        }
132
133 1
        if (!empty($priority)) {
134 1
            $params['priority'] = $priority;
135 1
        }
136
137 1
        $response = $this->httpPut(sprintf('/v3/routes/%s', $routeId), $params);
138
139 1
        return $this->hydrateResponse($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 2
    public function delete($routeId)
150
    {
151 2
        Assert::stringNotEmpty($routeId);
152
153 2
        $response = $this->httpDelete(sprintf('/v3/routes/%s', $routeId));
154
155 2
        return $this->hydrateResponse($response, DeleteResponse::class);
156
    }
157
}
158