Completed
Push — master ( 4a903b...b725ab )
by Tobias
03:23 queued 53s
created

Route::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
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
    public function index(int $limit = 100, int $skip = 0)
35
    {
36
        Assert::greaterThan($limit, 0);
37
        Assert::greaterThanEq($skip, 0);
38
39
        $params = [
40
            'limit' => $limit,
41
            'skip' => $skip,
42
        ];
43
44
        $response = $this->httpGet('/v3/routes', $params);
45
46
        return $this->hydrateResponse($response, IndexResponse::class);
47
    }
48
49
    /**
50
     * Returns a single Route object based on its ID.
51
     *
52
     * @param string $routeId Route ID returned by the Routes::index() method
53
     *
54
     * @return ShowResponse
55
     */
56
    public function show(string $routeId)
57
    {
58
        Assert::stringNotEmpty($routeId);
59
60
        $response = $this->httpGet(sprintf('/v3/routes/%s', $routeId));
61
62
        return $this->hydrateResponse($response, ShowResponse::class);
63
    }
64
65
    /**
66
     * Creates a new Route.
67
     *
68
     * @param string $expression  A filter expression like "match_recipient('.*@gmail.com')"
69
     * @param array  $actions     Route action. This action is executed when the expression evaluates to True. Example: "forward('[email protected]')"
70
     * @param string $description An arbitrary string
71
     * @param int    $priority    Integer: smaller number indicates higher priority. Higher priority routes are handled first. Defaults to 0.
72
     *
73
     * @return CreateResponse
74
     */
75 1
    public function create(string $expression, array $actions, string $description, int $priority = 0)
76
    {
77 1
        Assert::isArray($actions);
78
79
        $params = [
80 1
            'priority' => $priority,
81 1
            'expression' => $expression,
82 1
            'action' => $actions,
83 1
            'description' => $description,
84
        ];
85
86 1
        $response = $this->httpPost('/v3/routes', $params);
87
88 1
        return $this->hydrateResponse($response, CreateResponse::class);
89
    }
90
91
    /**
92
     * Updates a given Route by ID. All parameters are optional.
93
     * This API call only updates the specified fields leaving others unchanged.
94
     *
95
     * @param string      $routeId     Route ID returned by the Routes::index() method
96
     * @param string|null $expression  A filter expression like "match_recipient('.*@gmail.com')"
97
     * @param array|null  $actions     Route action. This action is executed when the expression evaluates to True. Example: "forward('[email protected]')"
98
     * @param string|null $description An arbitrary string
99
     * @param int|null    $priority    Integer: smaller number indicates higher priority. Higher priority routes are handled first. Defaults to 0.
100
     *
101
     * @return UpdateResponse
102
     */
103
    public function update(
104
        string $routeId,
105
        string $expression = null,
106
        array $actions = [],
107
        string $description = null,
108
        int $priority = null
109
    ) {
110
        Assert::stringNotEmpty($routeId);
111
        $params = [];
112
113
        if (!empty($expression)) {
114
            $params['expression'] = trim($expression);
115
        }
116
117
        foreach ($actions as $action) {
118
            Assert::stringNotEmpty($action);
119
120
            $params['action'] = isset($params['action']) ? $params['action'] : [];
121
            $params['action'][] = $action;
122
        }
123
124
        if (!empty($description)) {
125
            $params['description'] = trim($description);
126
        }
127
128
        if (!empty($priority)) {
129
            $params['priority'] = $priority;
130
        }
131
132
        $response = $this->httpPut(sprintf('/v3/routes/%s', $routeId), $params);
133
134
        return $this->hydrateResponse($response, UpdateResponse::class);
135
    }
136
137
    /**
138
     * Deletes a Route based on the ID.
139
     *
140
     * @param string $routeId Route ID returned by the Routes::index() method
141
     *
142
     * @return DeleteResponse
143
     */
144
    public function delete(string $routeId)
145
    {
146
        Assert::stringNotEmpty($routeId);
147
148
        $response = $this->httpDelete(sprintf('/v3/routes/%s', $routeId));
149
150
        return $this->hydrateResponse($response, DeleteResponse::class);
151
    }
152
}
153