Completed
Pull Request — master (#249)
by David
13:24 queued 03:20
created

Routes::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 4
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
Duplication introduced by
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
41
        $params = [
42
            'limit' => $limit,
43
            'skip' => $skip,
44
        ];
45
46
        $response = $this->httpGet('/v3/routes', $params);
47
48
        return $this->safeDeserialize($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 RouteDto
57
     */
58
    public function show($routeId)
59
    {
60
        Assert::stringNotEmpty($routeId);
61
62
        $response = $this->httpGet(sprintf('/v3/routes/%s', $routeId));
63
64
        return $this->safeDeserialize($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 View Code Duplication
    public function create($expression, array $actions, $description, $priority = 0)
0 ignored issues
show
Duplication introduced by
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
        Assert::string($expression);
80
        Assert::isArray($actions);
81
        Assert::string($description);
82
        Assert::integer($priority);
83
84
        $params = [
85
            'priority' => $priority,
86
            'expression' => $expression,
87
            'action' => $actions,
88
            'description' => $description,
89
        ];
90
91
        $response = $this->httpPost('/v3/routes', $params);
92
93
        return $this->safeDeserialize($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
    public function update($routeId, $expression = null, array $actions = null, $description = null, $priority = null)
109
    {
110
        Assert::stringNotEmpty($routeId);
111
112
        $params = [];
113
114 View Code Duplication
        if (is_string($expression) && 0 < strlen($expression)) {
0 ignored issues
show
Duplication introduced by
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...
115
            $params['expression'] = trim($expression);
116
        }
117
118
        if (is_array($actions)) {
119
            foreach ($actions as $action) {
120
                Assert::stringNotEmpty($action);
121
122
                $params['action'] = isset($params['action']) ? $params['action'] : [];
123
                $params['action'][] = $action;
124
            }
125
        }
126
127 View Code Duplication
        if (is_string($description) && 0 < strlen($description)) {
0 ignored issues
show
Duplication introduced by
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...
128
            $params['description'] = trim($description);
129
        }
130
131
        if (is_int($priority)) {
132
            $params['priority'] = $priority;
133
        }
134
135
        $response = $this->httpPut(sprintf('/v3/routes/%s', $routeId), $params);
136
137
        return $this->safeDeserialize($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 View Code Duplication
    public function delete($routeId)
0 ignored issues
show
Duplication introduced by
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...
148
    {
149
        Assert::stringNotEmpty($routeId);
150
151
        $response = $this->httpDelete(sprintf('/v3/routes/%s', $routeId));
152
153
        return $this->safeDeserialize($response, DeleteResponse::class);
154
    }
155
}
156