Passed
Push — master ( cc8c81...f0282f )
by Christopher
04:33
created

TDataServicesType::isOK()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 4
Ratio 17.39 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 23
rs 8.5906
cc 6
eloc 15
nc 5
nop 1
1
<?php
2
namespace AlgoWeb\ODataMetadata\MetadataV3\edmx;
3
4
use AlgoWeb\ODataMetadata\IsOK;
5
use AlgoWeb\ODataMetadata\IsOKTraits\IsOKToolboxTrait;
6
use AlgoWeb\ODataMetadata\MetadataV3\edm\Schema;
7
8
class TDataServicesType extends IsOK
9
{
10
    use IsOKToolboxTrait;
11
    /**
12
     * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[] $schema
13
     */
14
    private $schema = [];
15
16
    /**
17
     * @property string $maxDataServiceVersion
18
     */
19
    private $maxDataServiceVersion;
20
21
    /**
22
     * @property string $dataServiceVersion
23
     */
24
    private $dataServiceVersion;
25
26
    public function __construct($maxDataServiceVersion = '3.0', $dataServiceVersion = '3.0')
27
    {
28 View Code Duplication
        if ('3.0' == $this->maxDataServiceVersion && '4.0' == $this->dataServiceVersion) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
29
            $msg = "Data service version cannot be greater than maximum service version";
30
            throw new \InvalidArgumentException($msg);
31
        }
32
        $this->setDataServiceVersion($dataServiceVersion);
33
        $this->setMaxDataServiceVersion($maxDataServiceVersion);
34
    }
35
36
    /**
37
     * Gets as MaxDataServiceVersion
38
     *
39
     * @return string
40
     */
41
    public function getMaxDataServiceVersion()
42
    {
43
        return $this->maxDataServiceVersion;
44
    }
45
46
    /**
47
     * Sets a new DataServiceVersion
48
     *
49
     * @param  string $maxDataServiceVersion
50
     * @return self
51
     */
52
    public function setMaxDataServiceVersion($maxDataServiceVersion)
53
    {
54
        $maxDataValid = ['3.0', '4.0'];
55
        if (!in_array($maxDataServiceVersion, $maxDataValid)) {
56
            $msg = "Maximum data service version must be 3.0 or 4.0";
57
            throw new \InvalidArgumentException($msg);
58
        }
59
        $this->maxDataServiceVersion = $maxDataServiceVersion;
60
        return $this;
61
    }
62
63
    /**
64
     * Gets as DataServiceVersion
65
     *
66
     * @return string
67
     */
68
    public function getDataServiceVersion()
69
    {
70
        return $this->dataServiceVersion;
71
    }
72
73
    /**
74
     * Sets a new DataServiceVersion
75
     *
76
     * @param  string $dataServiceVersion
77
     * @return self
78
     */
79
    public function setDataServiceVersion($dataServiceVersion)
80
    {
81
        $dataValid = ['1.0', '2.0', '3.0', '4.0'];
82
        if (!in_array($dataServiceVersion, $dataValid)) {
83
            $msg = "Data service version must be 1.0, 2.0, 3.0 or 4.0";
84
            throw new \InvalidArgumentException($msg);
85
        }
86
        $this->dataServiceVersion = $dataServiceVersion;
87
        return $this;
88
    }
89
    /**
90
     * Adds as schema
91
     *
92
     * @return self
93
     * @param  \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema $schema
94
     */
95
    public function addToSchema(Schema $schema)
96
    {
97
        $msg = null;
98
        if (!$schema->isOK($msg)) {
99
            throw new \InvalidArgumentException($msg);
100
        }
101
        $this->schema[] = $schema;
102
        return $this;
103
    }
104
105
    /**
106
     * isset schema
107
     *
108
     * @param  scalar $index
109
     * @return boolean
110
     */
111
    public function issetSchema($index)
112
    {
113
        return isset($this->schema[$index]);
114
    }
115
116
    /**
117
     * unset schema
118
     *
119
     * @param  scalar $index
120
     * @return void
121
     */
122
    public function unsetSchema($index)
123
    {
124
        unset($this->schema[$index]);
125
    }
126
127
    /**
128
     * Gets as schema
129
     *
130
     * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[]
131
     */
132
    public function getSchema()
133
    {
134
        return $this->schema;
135
    }
136
137
    /**
138
     * Sets a new schema
139
     *
140
     * @param  \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[] $dataServices
141
     * @return self
142
     */
143
    public function setSchema(array $dataServices)
144
    {
145
        if (!$this->isValidArrayOK(
146
            $dataServices,
147
            '\AlgoWeb\ODataMetadata\MetadataV3\edm\Schema'
148
        )
149
        ) {
150
            $msg = "Data services array not a valid array";
151
            throw new \InvalidArgumentException($msg);
152
        }
153
        $this->schema = $dataServices;
154
        return $this;
155
    }
156
157
    public function isOK(&$msg = null)
158
    {
159
        $dataValid = ['1.0', '2.0', '3.0', '4.0'];
160
        $maxDataValid = ['3.0', '4.0'];
161
        if (!in_array($this->maxDataServiceVersion, $maxDataValid)) {
162
            $msg = "Maximum data service version must be 3.0 or 4.0";
163
            return false;
164
        }
165
        if (!in_array($this->dataServiceVersion, $dataValid)) {
166
            $msg = "Data service version must be 1.0, 2.0, 3.0 or 4.0";
167
            return false;
168
        }
169 View Code Duplication
        if ('3.0' == $this->maxDataServiceVersion && '4.0' == $this->dataServiceVersion) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
170
            $msg = "Data service version cannot be greater than maximum service version";
171
            return false;
172
        }
173
174
        if (!$this->isValidArrayOK($this->schema, '\AlgoWeb\ODataMetadata\MetadataV3\edm\Schema', $msg, 1)) {
175
            return false;
176
        }
177
178
        return true;
179
    }
180
}
181