Path::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
namespace gossi\swagger;
3
4
use gossi\swagger\parts\ExtensionPart;
5
use gossi\swagger\parts\ParametersPart;
6
use phootwork\collection\CollectionUtils;
7
use phootwork\collection\Map;
8
use phootwork\collection\Set;
9
use phootwork\lang\Arrayable;
10
11
class Path extends AbstractModel implements Arrayable {
12
13
    use ParametersPart;
14
	use ExtensionPart;
15
16
	private $operations;
17
18
	/** @var string */
19
	private $path;
20
21 9
	public function __construct($path, $contents = []) {
22 9
		$this->path = $path;
23 9
		$this->operations = new Map();
24 9
		$this->parse($contents);
25 9
	}
26
27 9
	private function parse($contents) {
28 9
		$data = CollectionUtils::toMap($contents);
29
30 9
		foreach (Swagger::$METHODS as $method) {
31 9
			if ($data->has($method)) {
32 7
				$this->operations->set($method, new Operation($data->get($method)));
33 7
			}
34 9
		}
35
36
        // parts
37 9
        $this->parseParameters($data);
38 9
        $this->parseExtensions($data);
39 9
	}
40
41 8
	public function toArray() {
42 8
		return array_merge(
43 8
			CollectionUtils::toArrayRecursive($this->operations),
44 8
			CollectionUtils::toArrayRecursive($this->getExtensions())
45 8
		);
46
	}
47
48
	/**
49
	 * Returns this path
50
	 *
51
	 * @return string
52
	 */
53 1
	public function getPath() {
54 1
		return $this->path;
55
	}
56
57
	/**
58
	 * Gets the operation for the given method, creates one if none exists
59
	 *
60
	 * @param string $method
61
	 * @return Operation
62
	 */
63 2
	public function getOperation($method) {
64 2
		if (!$this->operations->has($method)) {
65 1
			$this->operations->set($method, new Operation());
66 1
		}
67
68 2
		return $this->operations->get($method);
69
	}
70
71
	/**
72
	 * Sets the operation for a method
73
	 *
74
	 * @param string $method
75
	 * @param Operation $operation
76
	 */
77
	public function setOperation($method, Operation $operation) {
78
		$this->operations->set($method, $operation);
79
	}
80
81
	/**
82
	 *
83
	 * @param string $method
84
	 * @return bool
85
	 */
86
	public function hasOperation($method) {
87
		return $this->operations->has($method);
88
	}
89
90
	/**
91
	 * Removes an operation for the given method
92
	 *
93
	 * @param string $method
94
	 */
95
	public function removeOperation($method) {
96
		$this->operations->remove($method);
97
	}
98
99
	/**
100
	 * Returns all methods for this path
101
	 *
102
	 * @return Set
103
	 */
104
	public function getMethods() {
105
		return $this->operations->keys();
106
	}
107
108
}
109