Completed
Pull Request — master (#249)
by David
10:15
created

Routes::update()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 29
Code Lines 15

Duplication

Lines 6
Ratio 20.69 %

Importance

Changes 0
Metric Value
dl 6
loc 29
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 15
nc 24
nop 5
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.
29
     *
30
     * @param int $limit Maximum number of records to return. (100 by default)
31
     * @param int $skip  Number of records to skip. (0 by default)
32
     *
33
     * @return IndexResponse
34
     */
35 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...
36
    {
37
        Assert::integer($limit);
38
        Assert::integer($skip);
39
        Assert::greaterThan($limit, 0);
40
        Assert::greaterThanEq($skip, 0);
41
42
        $params = [
43
            'limit' => $limit,
44
            'skip' => $skip,
45
        ];
46
47
        $response = $this->httpGet('/v3/routes', $params);
48
49
        return $this->safeDeserialize($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
    public function show($routeId)
60
    {
61
        Assert::stringNotEmpty($routeId);
62
63
        $response = $this->httpGet(sprintf('/v3/routes/%s', $routeId));
64
65
        return $this->safeDeserialize($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 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...
79
    {
80
        Assert::string($expression);
81
        Assert::isArray($actions);
82
        Assert::string($description);
83
        Assert::integer($priority);
84
85
        $params = [
86
            'priority' => $priority,
87
            'expression' => $expression,
88
            'action' => $actions,
89
            'description' => $description,
90
        ];
91
92
        $response = $this->httpPost('/v3/routes', $params);
93
94
        return $this->safeDeserialize($response, CreateResponse::class);
95
    }
96
97
    /**
98
     * Updates a given Route by ID. All parameters are optional.
99
     * This API call only updates the specified fields leaving others unchanged.
100
     *
101
     * @param string      $routeId     Route ID returned by the Routes::index() method
102
     * @param string|null $expression  A filter expression like "match_recipient('.*@gmail.com')"
103
     * @param array|null  $actions     Route action. This action is executed when the expression evaluates to True. Example: "forward('[email protected]')"
104
     * @param string|null $description An arbitrary string
105
     * @param int|null    $priority    Integer: smaller number indicates higher priority. Higher priority routes are handled first. Defaults to 0.
106
     *
107
     * @return UpdateResponse
108
     */
109
    public function update($routeId, $expression = null, array $actions = [], $description = null, $priority = null)
110
    {
111
        Assert::stringNotEmpty($routeId);
112
113
        $params = [];
114
115 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...
116
            $params['expression'] = trim($expression);
117
        }
118
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 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...
127
            $params['description'] = trim($description);
128
        }
129
130
        if (is_int($priority)) {
131
            $params['priority'] = $priority;
132
        }
133
134
        $response = $this->httpPut(sprintf('/v3/routes/%s', $routeId), $params);
135
136
        return $this->safeDeserialize($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 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...
147
    {
148
        Assert::stringNotEmpty($routeId);
149
150
        $response = $this->httpDelete(sprintf('/v3/routes/%s', $routeId));
151
152
        return $this->safeDeserialize($response, DeleteResponse::class);
153
    }
154
}
155