Completed
Push — master ( 37902c...ffb897 )
by Martijn
02:31
created

Path   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 100
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C handleCommand() 0 30 7
A toArray() 0 12 1
A __toString() 0 5 1
A hasOperationId() 0 10 3
1
<?php
2
3
namespace SwaggerGen\Swagger;
4
5
/**
6
 * Describes a Swagger Path object, containing any number of operations
7
 * belonging to a single endpoint defined by this class.
8
 *
9
 * @package    SwaggerGen
10
 * @author     Martijn van der Lee <[email protected]>
11
 * @copyright  2014-2015 Martijn van der Lee
12
 * @license    https://opensource.org/licenses/MIT MIT
13
 */
14
class Path extends AbstractObject
15
{
16
17
	private static $methods = array(
18
		'get',
19
		'post',
20
		'put',
21
		'patch',
22
		'delete',
23
	);
24
25
	/**
26
	 * @var Operation[] $operation
27
	 */
28
	private $operations = array();
29
30
	/**
31
	 * @var Tag|null $tag;
32
	 */
33
	private $tag;
34
35
	public function __construct(AbstractObject $parent, Tag $Tag = null)
36
	{
37
		parent::__construct($parent);
38
		$this->tag = $Tag;
39
	}
40
41
	/**
42
	 * @param string $command
43
	 * @param string $data
44
	 * @return \SwaggerGen\Swagger\AbstractObject|boolean
45
	 */
46
	public function handleCommand($command, $data = null)
47
	{
48
		switch (strtolower($command)) {
49
			case 'method': // alias
50
			case 'operation':
51
				$method = strtolower(self::wordShift($data));
52
53
				if (!in_array($method, self::$methods)) {
54
					throw new \SwaggerGen\Exception("Unrecognized operation method '{$method}'.");
55
				}
56
57
				if (isset($this->operations[$method])) {
58
					$Operation = $this->operations[$method];
59
				} else {
60
					$summary = $data;
61
					$Operation = new Operation($this, $summary, $this->tag);
62
					$this->operations[$method] = $Operation;
63
				}
64
65
				return $Operation;
66
67
			case 'description':
68
				if ($this->tag) {
69
					return $this->tag->handleCommand($command, $data);
70
				}
71
				break;
72
		}
73
74
		return parent::handleCommand($command, $data);
75
	}
76
77
	public function toArray()
78
	{
79
		$methods = self::$methods;
80
		uksort($this->operations, function($a, $b) use ($methods) {
81
			return array_search($a, $methods) - array_search($b, $methods);
82
		});
83
84
		return self::arrayFilterNull(array_merge(
85
								self::objectsToArray($this->operations)
86
								, parent::toArray())
87
		);
88
	}
89
90
	public function __toString()
91
	{
92
		end($this->operations);
93
		return __CLASS__ . ' ' . key($this->operations);
94
	}
95
96
	/**
97
	 * Check if an operation with the given id is registered to this Path.
98
	 * 
99
	 * @param string $operationId
100
	 * @return boolean
101
	 */
102
	public function hasOperationId($operationId)
103
	{
104
		foreach ($this->operations as $operation) {
105
			if ($operation->getId() === $operationId) {
106
				return true;
107
			}
108
		}
109
110
		return false;
111
	}
112
113
}
114