Completed
Push — master ( 0f5433...3fbd33 )
by David
01:51
created

Route::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
 * {@link 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
41
        $params = [
42 1
            'limit' => $limit,
43 1
            'skip' => $skip,
44
        ];
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(string $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 1
    public function create(string $expression, array $actions, string $description, int $priority = 0)
78
    {
79 1
        Assert::isArray($actions);
80
81
        $params = [
82 1
            'priority' => $priority,
83 1
            'expression' => $expression,
84 1
            'action' => $actions,
85 1
            'description' => $description,
86
        ];
87
88 1
        $response = $this->httpPost('/v3/routes', $params);
89
90 1
        return $this->hydrateResponse($response, CreateResponse::class);
91
    }
92
93
    /**
94
     * Updates a given Route by ID. All parameters are optional.
95
     * This API call only updates the specified fields leaving others unchanged.
96
     *
97
     * @param string      $routeId     Route ID returned by the Routes::index() method
98
     * @param string|null $expression  A filter expression like "match_recipient('.*@gmail.com')"
99
     * @param array       $actions     Route action. This action is executed when the expression evaluates to True. Example: "forward('[email protected]')"
100
     * @param string|null $description An arbitrary string
101
     * @param int|null    $priority    Integer: smaller number indicates higher priority. Higher priority routes are handled first. Defaults to 0.
102
     *
103
     * @return UpdateResponse
104
     */
105 1
    public function update(
106
        string $routeId,
107
        string $expression = null,
108
        array $actions = [],
109
        string $description = null,
110
        int $priority = null
111
    ) {
112 1
        Assert::stringNotEmpty($routeId);
113 1
        $params = [];
114
115 1
        if (!empty($expression)) {
116 1
            $params['expression'] = trim($expression);
117
        }
118
119 1
        foreach ($actions as $action) {
120 1
            Assert::stringNotEmpty($action);
121
122 1
            $params['action'] = isset($params['action']) ? $params['action'] : [];
123 1
            $params['action'][] = $action;
124
        }
125
126 1
        if (!empty($description)) {
127 1
            $params['description'] = trim($description);
128
        }
129
130 1
        if (!empty($priority)) {
131 1
            $params['priority'] = $priority;
132
        }
133
134 1
        $response = $this->httpPut(sprintf('/v3/routes/%s', $routeId), $params);
135
136 1
        return $this->hydrateResponse($response, UpdateResponse::class);
137
    }
138
139
    /**
140
     * Deletes a Route based on the ID.
141
     *
142
     * @param string $routeId Route ID returned by the Routes::index() method
143
     *
144
     * @return DeleteResponse
145
     */
146 1
    public function delete(string $routeId)
147
    {
148 1
        Assert::stringNotEmpty($routeId);
149
150 1
        $response = $this->httpDelete(sprintf('/v3/routes/%s', $routeId));
151
152 1
        return $this->hydrateResponse($response, DeleteResponse::class);
153
    }
154
}
155