Completed
Push — master ( f8b47e...7f9080 )
by Thomas
04:06
created

Paths::addAll()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
namespace gossi\swagger\collections;
3
4
use gossi\swagger\parts\ExtensionPart;
5
use gossi\swagger\Path;
6
use phootwork\collection\CollectionUtils;
7
use phootwork\collection\Map;
8
use phootwork\lang\Arrayable;
9
use phootwork\lang\Text;
10
use gossi\swagger\AbstractModel;
11
12
class Paths extends AbstractModel implements Arrayable, \Iterator {
13
14
	use ExtensionPart;
15
16
	/** @var Map */
17
	private $paths;
18
19 10
	public function __construct($contents = []) {
20 10
		$this->parse($contents === null ? [] : $contents);
21 10
	}
22
23 10
	private function parse($contents) {
24 10
		$data = CollectionUtils::toMap($contents);
25
26
		// paths
27 10
		$this->paths = new Map();
28 10
		foreach ($data as $p => $path) {
29 6
			if (!Text::create($p)->startsWith('x-')) {
30 6
				$this->paths->set($p, new Path($p, $path));
31 6
			}
32 10
		}
33
34
		// extensions
35 10
		$this->parseExtensions($data);
36 10
	}
37
38 8
	public function toArray() {
39
// 		$paths = clone $this->paths;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
// 		$paths->setAll($this->getExtensions());
41
// 		return $paths->toArray();
42 8
		return CollectionUtils::toArrayRecursive($this->paths);
43
	}
44
45 1
	public function size() {
46 1
		return $this->paths->size();
47
	}
48
49
	/**
50
	 * Returns whether a path with the given name exists
51
	 *
52
	 * @param string $path
53
	 * @return bool
54
	 */
55 1
	public function has($path) {
56 1
		return $this->paths->has($path);
57
	}
58
59
	/**
60
	 * Returns whether the given path exists
61
	 *
62
	 * @param Path $path
63
	 * @return bool
64
	 */
65 1
	public function contains(Path $path) {
66 1
		return $this->paths->contains($path);
67
	}
68
69
	/**
70
	 * Returns the path info for the given path
71
	 *
72
	 * @param string $path
73
	 * @return Path
74
	 */
75 2
	public function get($path) {
76 2
		if (!$this->paths->has($path)) {
77
			$this->paths->set($path, new Path($path));
78
		}
79 2
		return $this->paths->get($path);
80
	}
81
82
	/**
83
	 * Sets the path
84
	 *
85
	 * @param Path $path
86
	 * @return $this
87
	 */
88 1
	public function add(Path $path) {
89 1
		$this->paths->set($path->getPath(), $path);
90 1
		return $this;
91
	}
92
93
	/**
94
	 * Adds all operations from another paths collection. Will overwrite existing operations.
95
	 *
96
	 * @param Paths $paths
97
	 */
98
	public function addAll(Paths $paths) {
99
		foreach ($paths as $p) {
100
			$path = $this->get($p->getPath());
101
			foreach ($p->getMethods() as $method) {
102
				$path->setOperation($method, $p->getOperation($method));
103
			}
104
		}
105
	}
106
107
	/**
108
	 * Removes the given path
109
	 *
110
	 * @param string $path
111
	 */
112 1
	public function remove($path) {
113 1
		$this->paths->remove($path);
114 1
		return $this;
115
	}
116
117
	public function current() {
118
		return $this->paths->current();
119
	}
120
121
	public function key() {
122
		return $this->paths->key();
123
	}
124
125
	public function next() {
126
		return $this->paths->next();
127
	}
128
129
	public function rewind() {
130
		return $this->paths->rewind();
131
	}
132
133
	public function valid() {
134
		return $this->paths->valid();
135
	}
136
}
137