GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#3)
by Igor
05:14
created

OptimizationProblem::removeOptimization()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 1
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Route4Me;
4
5
use Route4Me\Address;
6
use Route4Me\Common;
7
use Route4Me\RouteParameters;
8
use Route4Me\OptimizationProblemParams;
9
use Route4Me\Route;
10
use Route4Me\Route4Me;
11
use GuzzleHttp\Client;
12
13
class OptimizationProblem extends Common
14
{
15
    static public $apiUrl = '/api.v4/optimization_problem.php';
16
	static public $apiUrl_addr = '/api.v4/address.php';
17
    static public $apiHybridUrl = '/api.v4/hybrid_date_optimization.php';
18
    static public $apiHybridDepotUrl = '/api/change_hybrid_optimization_depot.php';
19
20
    public $optimization_problem_id;
21
    public $user_errors = array();
22
    public $state;
23
    public $parameters;
24
    public $sent_to_background;
25
    public $addresses = array();
26
    public $routes = array();
27
    public $links = array();
28
29
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
    {
31
        $this->parameters = new RouteParameters;
32
    }
33
34
    public static function fromArray(array $params)
35
    {
36
        $problem = new OptimizationProblem;
37
        $problem->optimization_problem_id = Common::getValue($params, 'optimization_problem_id');
38
        $problem->user_errors = Common::getValue($params, 'user_errors', array());
39
        $problem->state = Common::getValue($params, 'state', array());
40
        $problem->sent_to_background = Common::getValue($params, 'sent_to_background', array());
41
        $problem->links = Common::getValue($params, 'links', array());
42
43
        if (isset($params['parameters'])) {
44
            $problem->parameters = RouteParameters::fromArray($params['parameters']);
45
        }
46
47 View Code Duplication
        if (isset($params['addresses'])) {
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...
48
            $addresses = array();
49
            foreach ($params['addresses'] as $address) {
50
                $addresses[] = Address::fromArray($address);
51
            }
52
            $problem->addresses = $addresses;
53
        }
54
55
        if (isset($params['routes'])) {
56
            $routes = array();
57
            foreach ($params['routes'] as $route) {
58
                $routes[] = Route::fromArray($route);
59
            }
60
            $problem->routes = $routes;
61
        }
62
63
        return $problem;
64
    }
65
66
    public static function optimize(OptimizationProblemParams $params)
67
    {
68
        $optimize = Route4Me::makeRequst(array(
69
            'url'    => self::$apiUrl,
70
            'method' => 'POST',
71
            'query'  => array(
72
                'redirect'               => isset($params->redirect) ? $params->redirect : null,
73
                'directions'             => isset($params->directions) ? $params->directions: null, 
74
                'format'                 => isset($params->format) ? $params->format: null,
75
                'route_path_output'      => isset($params->route_path_output) ? $params->route_path_output: null,
76
                'optimized_callback_url' => isset($params->optimized_callback_url) ? $params->optimized_callback_url: null
77
            ),
78
            'body'   => array(
79
                'addresses'  => $params->getAddressesArray(),
80
                'parameters' => $params->getParametersArray()
81
            )
82
        ));
83
84
        return OptimizationProblem::fromArray($optimize);
85
    }
86
87
    public static function get($params)
88
    {
89
        $optimize = Route4Me::makeRequst(array(
90
            'url'    => self::$apiUrl,
91
            'method' => 'GET',
92
            'query'  => array(
93
                'state' => isset($params['state']) ? $params['state'] : null,
94
                'limit' => isset($params['limit']) ? $params['limit'] : null,
95
                'offset' => isset($params['offset']) ? $params['offset'] : null,
96
                'optimization_problem_id' => isset($params['optimization_problem_id']) 
97
                    ? $params['optimization_problem_id'] : null,
98
                'wait_for_final_state' => isset($params['wait_for_final_state']) 
99
                    ? $params['wait_for_final_state'] : null,
100
            )
101
        ));
102
103
        if (isset($optimize['optimizations'])) {
104
            $problems = array();
105
            foreach($optimize['optimizations'] as $problem) {
106
                $problems[] = OptimizationProblem::fromArray($problem);
107
            }
108
            return $problems;
109
        } else {
110
            return OptimizationProblem::fromArray($optimize);
111
        }
112
    }
113
114
    public static function reoptimize($params)
115
    {
116
        $param = new OptimizationProblemParams;
117
        $param->optimization_problem_id = isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null;
118
        $param->reoptimize = 1;
119
120
        return self::update((array)$param);
121
    }
122
123
    public static function update($params)
124
    {
125
		$optimize = Route4Me::makeRequst(array(
126
            'url'    => self::$apiUrl,
127
            'method' => 'PUT',
128
            'query'  => array(
129
                'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
130
                'addresses' => isset($params['addresses']) ? $params['addresses'] : null,
131
                'reoptimize' => isset($params['reoptimize']) ? $params['reoptimize'] : null,
132
            )
133
        ));
134
		
135
		return $optimize;
136
    }
137
138
    public function getOptimizationId()
139
    {
140
        return $this->optimization_problem_id;
141
    }
142
143
    public function getRoutes()
144
    {
145
        return $this->routes;
146
    }
147
	
148
	public function getRandomOptimizationId($offset,$limit)
0 ignored issues
show
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
	{
150
		$query['limit'] = isset($params['limit']) ? $params['limit'] : 30;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Bug introduced by
The variable $params seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
151
        $query['offset'] = isset($params['offset']) ? $params['offset'] : 0;
152
			
153
		$json = Route4Me::makeRequst(array(
154
            'url'    => self::$apiUrl,
155
            'method' => 'GET',
156
            'query'  => $query
157
        ));
158
		
159
		$optimizations = array();
160
            foreach($json as $optimization) {
161
				if (gettype($optimization)!="array") continue;
162
				foreach ($optimization as $otp1) {
163
					$optimizations[] = $otp1;
164
				}
165
            }
166
			
167
			$num=rand(0,sizeof($optimizations)-1);
168
			//echo "num=$num.<br>".sizeof($optimizations)."<br>";
169
			$rOptimization=$optimizations[$num];
170
			return $rOptimization;
171
	}
172
	
173
	public function getAddresses($opt_id)
174
	{
175
		if ($opt_id==null) return null;
176
		
177
		$params = array( "optimization_problem_id" => $opt_id );
178
		
179
		$optimization = (array)$this->get($params);
180
		
181
		$addresses = $optimization["addresses"];
182
		
183
		return $addresses;
184
		
185
	}
186
	
187
	public function getRandomAddressFromOptimization($opt_id)
188
	{
189
		$addresses = (array)$this->getAddresses($opt_id);
190
		
191
		if ($addresses == null) {
192
			echo "There are no addresses in this optimization!.. Try again.";
193
			return null;
194
		}
195
		
196
		$num=rand(0,sizeof($addresses)-1);
197
		$rAddress=$addresses[$num];
198
		
199
		return $rAddress;
200
	}
201
	
202 View Code Duplication
	public function removeAddress($params)
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...
203
	{
204
		$response = Route4Me::makeRequst(array(
205
            'url'    => self::$apiUrl_addr,
206
            'method' => 'DELETE',
207
            'query'  => array(
208
                'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
209
                'route_destination_id' => isset($params['route_destination_id']) ? $params['route_destination_id'] : null,
210
            )
211
        ));
212
		
213
		return $response;
214
	}
215
	
216 View Code Duplication
	public function removeOptimization($params)
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...
217
	{
218
		$response = Route4Me::makeRequst(array(
219
            'url'    => self::$apiUrl,
220
            'method' => 'DELETE',
221
            'query'  => array(
222
                'redirect' => isset($params['redirect']) ? $params['redirect'] : null,
223
            ),
224
            'body'  => array(
225
				'optimization_problem_ids' => isset($params['optimization_problem_ids']) ? $params['optimization_problem_ids'] : null,
226
			)
227
        ));
228
		
229
		return $response;
230
	}
231
    
232 View Code Duplication
    public function getHybridOptimization($params)
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...
233
    {
234
        $optimize = Route4Me::makeRequst(array(
235
            'url'    => self::$apiHybridUrl,
236
            'method' => 'GET',
237
            'query'  => array(
238
                'target_date_string' => isset($params['target_date_string']) ? $params['target_date_string'] : null,
239
                'timezone_offset_minutes' => isset($params['timezone_offset_minutes']) ? $params['timezone_offset_minutes'] : null
240
            )
241
        ));
242
243
        return $optimize;
244
    }
245
    
246 View Code Duplication
    Public function addDepotsToHybrid($params)
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...
247
    {
248
        $depots = Route4Me::makeRequst(array( 
249
            'url'    => self::$apiHybridDepotUrl,
250
            'method' => 'POST',
251
            'query'  => array(
252
                'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
253
                ),
254
            'body'  => array(
255
                'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
256
                'delete_old_depots' => isset($params['delete_old_depots']) ? $params['delete_old_depots'] : null,
257
                'new_depots' => isset($params['new_depots']) ? $params['new_depots'] : null,
258
            )
259
        ));
260
        
261
        return $depots;
262
    }
263
}
264