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.
Completed
Push — master ( 3cd911...18fe79 )
by Petr
02:50 queued 48s
created

MapAPI::getDirection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2015 Petr Olišar (http://olisar.eu)
4
 *
5
 * For the full copyright and license information, please view
6
 * the file LICENSE.md that was distributed with this source code.
7
 */
8
9
namespace Oli\GoogleAPI;
10
11
use Nette\Application\UI\Control;
12
use Nette\Application\Responses\JsonResponse;
13
use Nette\Application\UI\ITemplate;
14
use Nette\Utils\Json;
15
16
17
/**
18
 * Description of GoogleAPI
19
 *
20
 * @author Petr Olišar <[email protected]>
21
 * @property-read ITemplate $template
22
 */
23
class MapAPI extends Control
24 1
{
25
26
	const ROADMAP = 'ROADMAP', SATELLITE = 'SATELLITE', HYBRID = 'HYBRID', TERRAIN = 'TERRAIN',
27
		BICYCLING = 'BICYCLING', DRIVING = 'DRIVING', TRANSIT = 'TRANSIT', WALKING = 'WALKING';
28
29
	/**
30
	 * @var double|string
31
	 */
32
	private $width;
33
34
	/**
35
	 * @var double|string
36
	 */
37
	private $height;
38
39
	/**
40
	 * @var array
41
	 */
42
	private $coordinates;
43
44
	/**
45
	 * @var int
46
	 */
47
	private $zoom;
48
49
	/**
50
	 * @var string
51
	 */
52
	private $type;
53
54
	/**
55
	 * @var bool
56
	 */
57
	private $staticMap = FALSE;
58
59
	/**
60
	 * @var bool
61
	 */
62
	private $clickable = FALSE;
63
64
	/**
65
	 * @var string
66
	 */
67
	private $key;
68
69
	/**
70
	 * @var array
71
	 */
72
	private $markers = array();
73
74
	/**
75
	 * @var bool
76
	 */
77
	private $bound;
78
79
	/**
80
	 * @var bool
81
	 */
82
	private $markerClusterer;
83
84
	/**
85
	 * @var array
86
	 */
87
	private $clusterOptions;
88
89
	/**
90
	 * @var bool
91
	 */
92
	private $scrollable = FALSE;
93
94
	/**
95
	 * @var array
96
	 */
97
	private $waypoints;
98
99
	/**
100
	 * @var array
101
	 */
102
	private $direction = ['travelmode' => 'DRIVING'];
103
104
105
	public function __construct()
106
	{
107
108 1
	}
109
110
111
	/**
112
	 * @param array $config
113
	 * @internal
114
	 */
115
	public function setup(array $config)
116
	{
117 1
		$this->width = $config['width'];
118 1
		$this->height = $config['height'];
119 1
	}
120
121
122
	/**
123
	 * @param array $coordinates	(latitude, longitude) - center of the map
124
	 * @return $this
125
	 */
126
	public function setCoordinates(array $coordinates)
127
	{
128 1
		if(!count($coordinates))
129 1
		{
130 1
			$this->coordinates = array(NULL, NULL);
131 1
		} else
132
		{
133 1
			$this->coordinates = array_values($coordinates);
134
		}
135
		
136 1
		return $this;
137
	}
138
139
140
	/**
141
	 * @param double|string $width
142
	 * @param double|string $height
143
	 * @return $this
144
	 */
145
	public function setProportions($width, $height)
146
	{
147 1
		$this->width = $width;
148 1
		$this->height = $height;
149 1
		return $this;
150
	}
151
152
153
	/**
154
	 * @param string $key
155
	 * @return $this
156
	 */
157
	public function setKey($key)
158
	{
159 1
		$this->key = $key;
160 1
		return $this;
161
	}
162
163
164
	/**
165
	 * @param int $zoom		<0, 19>
166
	 * @return $this
167
	 * @throws InvalidArgumentException
168
	 * @throws LogicException
169
	 */
170
	public function setZoom($zoom)
171
	{
172 1
		if (!is_int($zoom))
173 1
		{
174 1
			throw new InvalidArgumentException("type must be integer, $zoom (".gettype($zoom).") was given");
175
		}
176
177 1
		if ($zoom < 0 || $zoom > 19)
178 1
		{
179 1
			throw new LogicException('Zoom must be betwen <0, 19>.');
180
		}
181
182 1
		$this->zoom = (int) $zoom;
183 1
		return $this;
184
	}
185
186
187
	/**
188
	 * @param string $type
189
	 * @return $this
190
	 * @throws InvalidArgumentException
191
	 */
192
	public function setType($type)
193
	{
194 1
		if($type !== self::HYBRID && $type !== self::ROADMAP && $type !== self::SATELLITE &&
195 1
				$type !== self::TERRAIN)
196 1
		{
197 1
			throw new InvalidArgumentException;
198
		}
199 1
		$this->type = $type;
200 1
		return $this;
201
	}
202
203
204
	/**
205
	 * @param string $key
206
	 * @param array $waypoint
207
	 * @return $this
208
	 * @throws InvalidArgumentException
209
	 */
210
	public function setWaypoint($key, array $waypoint)
211
	{
212 1
		if (!in_array($key, ['start', 'end', 'waypoint']))
213 1
		{
214 1
			throw new InvalidArgumentException;
215
		}
216
217 1
		if($key === 'waypoint')
218 1
		{
219 1
			$this->waypoints['waypoints'][] = $waypoint;
220
221 1
		} else
222
		{
223 1
			$this->waypoints[$key] = $waypoint;
224
		}
225 1
		return $this;
226
	}
227
228
229
	/**
230
	 * @param array $direction
231
	 * @return $this
232
	 * @throws InvalidArgumentException
233
	 */
234
	public function setDirection(array $direction)
235
	{
236 1
		$this->direction = $direction;
237 1
		if(!array_key_exists('travelmode', $this->direction))
238 1
		{
239 1
			$this->direction['travelmode'] = 'DRIVING';
240 1
		} else if (!in_array($direction['travelmode'], [
241 1
			self::BICYCLING, self::DRIVING, self::WALKING, self::TRANSIT
242 1
		]))
243 1
		{
244 1
			throw new InvalidArgumentException;
245
		}
246 1
		return $this;
247
	}
248
249
250
	/**
251
	 * @return array	Width and Height of the map.
252
	 */
253
	public function getProportions()
254
	{
255 1
		return array('width' => $this->width, 'height' => $this->height);
256
	}
257
258
259
	/**
260
	 * @return array	Center of the map
261
	 */
262
	public function getCoordinates()
263
	{
264 1
		return $this->coordinates;
265
	}
266
267
268
	/**
269
	 * @return int
270
	 */
271
	public function getZoom()
272
	{
273 1
		return $this->zoom;
274
	}
275
276
277
	/**
278
	 * @return string	Which map type will be show
279
	 */
280
	public function getType()
281
	{
282 1
		return $this->type;
283
	}
284
285
286
	/**
287
	 * @return array
288
	 */
289
	public function getWaypoints()
290
	{
291 1
		return $this->waypoints;
292
	}
293
294
295
	/**
296
	 * @return array
297
	 */
298
	public function getDirection()
299
	{
300 1
	    return $this->direction;
301
	}
302
303
304
	/**
305
	 * @return string
306
	 */
307
	public function getKey()
308
	{
309 1
		return $this->key;
310
	}
311
312
313
	/**
314
	 * @param bool $staticMap
315
	 * @return $this
316
	 * @throws InvalidArgumentException
317
	 */
318 View Code Duplication
	public function isStaticMap($staticMap = TRUE)
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...
319
	{
320 1
		if (!is_bool($staticMap))
321 1
		{
322 1
			throw new InvalidArgumentException("staticMap must be boolean, $staticMap (".gettype($staticMap).") was given");
323
		}
324
325 1
		$this->staticMap = $staticMap;
326 1
		return $this;
327
	}
328
329
330
	/**
331
	 * @return bool
332
	 */
333
	public function getIsStaticMap()
334
	{
335 1
		return $this->staticMap;
336
	}
337
338
339
	/**
340
	 * @param bool $clickable
341
	 * @return $this
342
	 * @throws InvalidArgumentException
343
	 */
344 View Code Duplication
	public function isClickable($clickable = TRUE)
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...
345
	{
346 1
		if (!$this->staticMap)
347 1
		{
348 1
			throw new InvalidArgumentException("the 'clickable' option only applies to static map");
349
		}
350
351 1
		if (!is_bool($clickable))
352 1
		{
353 1
			throw new InvalidArgumentException("clickable must be boolean, $clickable (".gettype($clickable).") was given");
354
		}
355
356 1
		$this->clickable = $clickable;
357 1
		return $this;
358
	}
359
360
361
	/**
362
	 * @return bool
363
	 */
364
	public function getIsClicable()
365
	{
366 1
		return $this->clickable;
367
	}
368
369
370
	/**
371
	 * @param bool $scrollable
372
	 * @return $this
373
	 * @throws InvalidArgumentException
374
	 */
375 View Code Duplication
	public function isScrollable($scrollable = TRUE)
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...
376
	{
377 1
		if (!is_bool($scrollable))
378 1
		{
379 1
			throw new InvalidArgumentException("staticMap must be boolean, $scrollable (".gettype($scrollable).") was given");
380
		}
381
		
382 1
		$this->scrollable = $scrollable;
383 1
		return $this;
384
	}
385
386
387
	/**
388
	 * @return bool
389
	 */
390
	public function getIsScrollable()
391
	{
392 1
		return $this->scrollable;
393
	}
394
395
396
	/**
397
	 * @param Markers $markers
398
	 * @return $this
399
	 */
400
	public function addMarkers(Markers $markers)
401
	{
402
		$this->markers = $markers->getMarkers();
403
		$this->bound = $markers->getBound();
404
		$this->markerClusterer = $markers->getMarkerClusterer();
405
		$this->clusterOptions = $markers->getClusterOptions();
406
		return $this;
407
	}
408
409
410
	/**
411
	 * Alias to handleMarkers()
412
	 */
413
	public function invalidateMarkers()
414
	{
415
		$this->handleMarkers();
416
	}
417
418
419
	/**
420
	 * @throws \Nette\Utils\JsonException
421
	 */
422
	public function render()
423
	{
424
		if ($this->staticMap)
425
		{
426
			$this->template->height = $this->height;
0 ignored issues
show
Bug introduced by
Accessing height on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
427
			$this->template->width = $this->width;
0 ignored issues
show
Bug introduced by
Accessing width on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
428
			$this->template->zoom = $this->zoom;
0 ignored issues
show
Bug introduced by
Accessing zoom on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
429
			$this->template->position = $this->coordinates;
0 ignored issues
show
Bug introduced by
Accessing position on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
430
			$this->template->markers = $this->markers;
0 ignored issues
show
Bug introduced by
Accessing markers on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
431
			$this->template->clickable = $this->clickable;
0 ignored issues
show
Bug introduced by
Accessing clickable on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
432
			$this->template->setFile(dirname(__FILE__) . '/static.latte');
433
		} else
434
		{
435
			$map = array(
436
			    'position' => $this->coordinates,
437
			    'height' => $this->height,
438
			    'width' => $this->width,
439
			    'zoom' => $this->zoom,
440
			    'type' => $this->type,
441
			    'scrollable' => $this->scrollable,
442
			    'key' => $this->key,
443
			    'bound' => $this->bound,
444
			    'cluster' => $this->markerClusterer,
445
			    'clusterOptions' => $this->clusterOptions,
446
			    'waypoint' => !is_null($this->waypoints) ? array_merge($this->waypoints, $this->direction) : NULL
447
			);
448
			$this->template->map = Json::encode($map);
0 ignored issues
show
Bug introduced by
Accessing map on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
449
			$this->template->setFile(dirname(__FILE__) . '/template.latte');
450
		}
451
		$this->template->render();
452
	}
453
454
455
	/**
456
	 * Send markers to template as JSON
457
	 * @internal
458
	 */
459
	public function handleMarkers()
460
	{
461
		$this->getPresenter()->sendResponse(new JsonResponse($this->markers));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Nette\ComponentModel\IComponent as the method sendResponse() does only exist in the following implementations of said interface: Nette\Application\UI\Presenter.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
462
	}
463
}
464