| 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) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |         Assert::nullOrString($expression); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         Assert::isArray($actions); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         Assert::nullOrString($description); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         Assert::nullOrInteger($priority); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         $params = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         if (!empty($expression)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             $params['expression'] = trim($expression); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         foreach ($actions as $action) { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             Assert::stringNotEmpty($action); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             $params['action'] = isset($params['action']) ? $params['action'] : []; | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             $params['action'][] = $action; | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         if (!empty($description)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |             $params['description'] = trim($description); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         if (!empty($priority)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |             $params['priority'] = $priority; | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         $response = $this->httpPut(sprintf('/v3/routes/%s', $routeId), $params); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         return $this->safeDeserialize($response, UpdateResponse::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * Deletes a Route based on the ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param string $routeId Route ID returned by the Routes::index() method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @return DeleteResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 150 |  | View Code Duplication |     public function delete($routeId) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         Assert::stringNotEmpty($routeId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $response = $this->httpDelete(sprintf('/v3/routes/%s', $routeId)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         return $this->safeDeserialize($response, DeleteResponse::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |  | 
            
                        
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.