Completed
Push — master ( b659c5...c65770 )
by Michael
10:22
created

EveApiXmlData   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 52.83%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 225
ccs 28
cts 53
cp 0.5283
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setCacheInterval() 0 5 1
A addEveApiArgument() 0 5 1
A getCacheInterval() 0 4 1
A getEveApiArgument() 0 8 2
A getEveApiArguments() 0 4 1
A getEveApiName() 0 8 2
A getEveApiSectionName() 0 8 2
A getEveApiXml() 0 4 1
A getHash() 0 11 2
A hasEveApiArgument() 0 4 1
A setEveApiArguments() 0 11 3
A setEveApiName() 0 5 1
A setEveApiSectionName() 0 5 1
A setEveApiXml() 0 5 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains EveApiXmlData class.
5
 *
6
 * PHP version 7.0+
7
 *
8
 * LICENSE:
9
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
10
 * which can be used to access the Eve Online API data and place it into a
11
 * database.
12
 * Copyright (C) 2014-2017 Michael Cummings
13
 *
14
 * This program is free software: you can redistribute it and/or modify it
15
 * under the terms of the GNU Lesser General Public License as published by the
16
 * Free Software Foundation, either version 3 of the License, or (at your
17
 * option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22
 * for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public License
25
 * along with this program. If not, see
26
 * <http://spdx.org/licenses/LGPL-3.0.html>.
27
 *
28
 * You should be able to find a copy of this license in the COPYING-LESSER.md
29
 * file. A copy of the GNU GPL should also be available in the COPYING.md file.
30
 *
31
 * @copyright 2014-2017 Michael Cummings
32
 * @license   LGPL-3.0+
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Xml;
36
37
/**
38
 * Class EveApiXmlData
39
 */
40
class EveApiXmlData implements EveApiReadWriteInterface
41
{
42
    /**
43
     * Used to add item to arguments list.
44
     *
45
     * @param string $name
46
     * @param string $value
47
     *
48
     * @return self Fluent interface.
49
     */
50
    public function addEveApiArgument(string $name, string $value): self
51
    {
52
        $this->eveApiArguments[$name] = $value;
53
        return $this;
54
    }
55
    /**
56
     * Getter for cache interval.
57
     *
58
     * @return int
59
     * @throws \LogicException
60
     */
61
    public function getCacheInterval(): int
62
    {
63
        return $this->cacheInterval;
64
    }
65
    /**
66
     * Getter for an existing Eve API argument.
67
     *
68
     * @param string $name
69
     *
70
     * @return string
71
     * @throws \DomainException
72
     */
73
    public function getEveApiArgument(string $name): string
74
    {
75
        if (!array_key_exists($name, $this->eveApiArguments)) {
76
            $mess = 'Unknown argument ' . $name;
77
            throw new \DomainException($mess);
78
        }
79
        return $this->eveApiArguments[$name];
80
    }
81
    /**
82
     * Getter for Eve API argument list.
83
     *
84
     * @return string[]
85
     */
86 25
    public function getEveApiArguments(): array
87
    {
88 25
        return $this->eveApiArguments;
89
    }
90
    /**
91
     * Getter for name of Eve API.
92
     *
93
     * @return string
94
     * @throws \LogicException Throws exception if accessed before being set.
95
     */
96 39
    public function getEveApiName(): string
97
    {
98 39
        if (null === $this->eveApiName) {
99
            $mess = 'Tried to access Eve Api name before it was set';
100
            throw new \LogicException($mess);
101
        }
102 39
        return $this->eveApiName;
103
    }
104
    /**
105
     * Getter for name of Eve API section.
106
     *
107
     * @return string
108
     * @throws \LogicException Throws exception if accessed before being set.
109
     */
110 39
    public function getEveApiSectionName(): string
111
    {
112 39
        if (null === $this->eveApiSectionName) {
113
            $mess = 'Tried to access Eve Api section name before it was set';
114
            throw new \LogicException($mess);
115
        }
116 39
        return $this->eveApiSectionName;
117
    }
118
    /**
119
     * Getter for the actual Eve API XML received.
120
     *
121
     * @return string
122
     */
123 35
    public function getEveApiXml(): string
124
    {
125 35
        return $this->eveApiXml;
126
    }
127
    /**
128
     * Used to get a repeatable unique hash for any combination API name, section, and arguments.
129
     *
130
     * @return string
131
     * @throws \LogicException
132
     */
133 14
    public function getHash(): string
134
    {
135 14
        $hash = $this->getEveApiName() . $this->getEveApiSectionName();
136 14
        $arguments = $this->getEveApiArguments();
137 14
        unset($arguments['mask'], $arguments['rowCount']);
138 14
        ksort($arguments);
139 14
        foreach ($arguments as $key => $value) {
140
            $hash .= $key . $value;
141
        }
142 14
        return hash('md5', $hash);
143
    }
144
    /**
145
     * Used to check if an argument exists.
146
     *
147
     * @param string $name
148
     *
149
     * @return bool
150
     */
151 39
    public function hasEveApiArgument(string $name): bool
152
    {
153 39
        return array_key_exists($name, $this->eveApiArguments);
154
    }
155
    /**
156
     * Cache interval setter.
157
     *
158
     * @param int $value Caching interval in seconds.
159
     *
160
     * @return self Fluent interface.
161
     */
162
    public function setCacheInterval(int $value): self
163
    {
164
        $this->cacheInterval = (int)$value;
165
        return $this;
166
    }
167
    /**
168
     * Used to set a list of arguments used when forming request to Eve Api
169
     * server.
170
     *
171
     * Things like KeyID, vCode etc that are either required or optional for the
172
     * Eve API. See adder for example.
173
     *
174
     * Example:
175
     * <code>
176
     * <?php
177
     * $args = array( 'KeyID' => '1156', 'vCode' => 'abc123');
178
     * $api->setEveApiArguments($args);
179
     * ...
180
     * </code>
181
     *
182
     * @param string[] $values
183
     *
184
     * @return self Fluent interface.
185
     * @uses EveApiXmlData::addEveApiArgument()
186
     */
187
    public function setEveApiArguments(array $values): self
188
    {
189
        $this->eveApiArguments = [];
190
        if (0 === count($values)) {
191
            return $this;
192
        }
193
        foreach ($values as $name => $value) {
194
            $this->addEveApiArgument($name, $value);
195
        }
196
        return $this;
197
    }
198
    /**
199
     * Eve API name setter.
200
     *
201
     * @param string $value
202
     *
203
     * @return self Fluent interface.
204
     */
205 40
    public function setEveApiName(string $value): self
206
    {
207 40
        $this->eveApiName = $value;
208 40
        return $this;
209
    }
210
    /**
211
     * Eve API section name setter.
212
     *
213
     * @param string $value
214
     *
215
     * @return self Fluent interface.
216
     */
217 40
    public function setEveApiSectionName(string $value): self
218
    {
219 40
        $this->eveApiSectionName = $value;
220 40
        return $this;
221
    }
222
    /**
223
     * Sets the actual Eve API XML data received.
224
     *
225
     * @param string $xml Actual XML content.
226
     *
227
     * @return self Fluent interface.
228
     */
229 37
    public function setEveApiXml(string $xml = ''): self
230
    {
231 37
        $this->eveApiXml = $xml;
232 37
        return $this;
233
    }
234
    /**
235
     * Holds expected/calculated cache interval for the current API in seconds.
236
     *
237
     * @var int $cacheInterval
238
     */
239
    private $cacheInterval = 300;
240
    /**
241
     * List of API arguments.
242
     *
243
     * @var string[] $eveApiArguments
244
     */
245
    private $eveApiArguments = [];
246
    /**
247
     * Holds Eve API name.
248
     *
249
     * @var string $eveApiName
250
     */
251
    private $eveApiName;
252
    /**
253
     * Holds Eve API section name.
254
     *
255
     * @var string $eveApiSectionName
256
     */
257
    private $eveApiSectionName;
258
    /**
259
     * Holds the actual Eve API XML data.
260
     *
261
     * @var string $eveApiXml
262
     */
263
    private $eveApiXml = '';
264
}
265