Completed
Pull Request — master (#3)
by Guilh
02:22
created

Path::removeOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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