CorporationSheet   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 6
dl 0
loc 197
ccs 0
cts 140
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B preserverToCorporationSheet() 0 33 1
A preserverToDivisions() 0 19 1
B preserverToLogo() 0 24 1
A preserverToWalletDivisions() 0 19 1
D processEvents() 0 36 10
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains class CorporationSheet.
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) 2016-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
 * @author    Michael Cummings <[email protected]>
32
 * @copyright 2016-2017 Michael Cummings
33
 * @license   LGPL-3.0+
34
 */
35
namespace Yapeal\EveApi\Corp;
36
37
use Yapeal\EveApi\CommonEveApiTrait;
38
use Yapeal\Event\EveApiPreserverInterface;
39
use Yapeal\Event\MediatorInterface;
40
use Yapeal\Log\Logger;
41
use Yapeal\Sql\PreserverTrait;
42
use Yapeal\Xml\EveApiReadWriteInterface;
43
44
/**
45
 * Class CorporationSheet.
46
 */
47
class CorporationSheet implements EveApiPreserverInterface
48
{
49
    use CommonEveApiTrait, PreserverTrait;
50
51
    /**
52
     * Constructor
53
     */
54
    public function __construct()
55
    {
56
        $this->mask = 8;
57
        $this->preserveTos = [
58
            'preserveToCorporationSheet',
59
            'preserverToDivisions',
60
            'preserverToLogo',
61
            'preserverToWalletDivisions'
62
        ];
63
    }
64
    /**
65
     * @param EveApiReadWriteInterface $data
66
     *
67
     * @return void
68
     * @throws \DomainException
69
     * @throws \InvalidArgumentException
70
     * @throws \LogicException
71
     * @throws \UnexpectedValueException
72
     */
73
    protected function preserverToCorporationSheet(EveApiReadWriteInterface $data)
74
    {
75
        $tableName = 'corpCorporationSheet';
76
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
77
        $sql = $this->getCsq()
78
            ->getDeleteFromTableWithOwnerID($tableName, $ownerID);
79
        $this->getYem()
80
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
81
        $this->getPdo()
82
            ->exec($sql);
83
        $columnDefaults = [
84
            'allianceID' => '0',
85
            'allianceName' => '',
86
            'ceoID' => null,
87
            'ceoName' => null,
88
            'corporationID' => $ownerID,
89
            'corporationName' => null,
90
            'description' => null,
91
            'factionID' => '0',
92
            'factionName' => '',
93
            'memberCount' => null,
94
            'memberLimit' => '0',
95
            'shares' => null,
96
            'stationID' => null,
97
            'stationName' => null,
98
            'taxRate' => null,
99
            'ticker' => null,
100
            'url' => null
101
        ];
102
        $xPath = '//result/child::*[not(*|@*|self::dataTime)]';
103
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
104
        $this->valuesPreserveData($elements, $columnDefaults, $tableName);
105
    }
106
    /**
107
     * @param EveApiReadWriteInterface $data
108
     *
109
     * @return void
110
     * @throws \DomainException
111
     * @throws \InvalidArgumentException
112
     * @throws \LogicException
113
     * @throws \UnexpectedValueException
114
     */
115
    protected function preserverToDivisions(EveApiReadWriteInterface $data)
116
    {
117
        $tableName = 'corpDivisions';
118
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
119
        $sql = $this->getCsq()
120
            ->getDeleteFromTableWithOwnerID($tableName, $ownerID);
121
        $this->getYem()
122
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
123
        $this->getPdo()
124
            ->exec($sql);
125
        $columnDefaults = [
126
            'ownerID' => $ownerID,
127
            'accountKey' => null,
128
            'description' => null
129
        ];
130
        $xPath = '//divisions/row';
131
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
132
        $this->attributePreserveData($elements, $columnDefaults, $tableName);
133
    }
134
    /**
135
     * @param EveApiReadWriteInterface $data
136
     *
137
     * @return void
138
     * @throws \DomainException
139
     * @throws \InvalidArgumentException
140
     * @throws \LogicException
141
     * @throws \UnexpectedValueException
142
     */
143
    protected function preserverToLogo(EveApiReadWriteInterface $data)
144
    {
145
        $tableName = 'corpLogo';
146
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
147
        $sql = $this->getCsq()
148
            ->getDeleteFromTableWithOwnerID($tableName, $ownerID);
149
        $this->getYem()
150
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
151
        $this->getPdo()
152
            ->exec($sql);
153
        $columnDefaults = [
154
            'ownerID' => $ownerID,
155
            'color1' => null,
156
            'color2' => null,
157
            'color3' => null,
158
            'graphicID' => null,
159
            'shape1' => null,
160
            'shape2' => null,
161
            'shape3' => null
162
        ];
163
        $xPath = '//logo/*';
164
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
165
        $this->valuesPreserveData($elements, $columnDefaults, $tableName);
166
    }
167
    /**
168
     * @param EveApiReadWriteInterface $data
169
     *
170
     * @return void
171
     * @throws \DomainException
172
     * @throws \InvalidArgumentException
173
     * @throws \LogicException
174
     * @throws \UnexpectedValueException
175
     */
176
    protected function preserverToWalletDivisions(EveApiReadWriteInterface $data)
177
    {
178
        $tableName = 'corpWalletDivisions';
179
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
180
        $sql = $this->getCsq()
181
            ->getDeleteFromTableWithOwnerID($tableName, $ownerID);
182
        $this->getYem()
183
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
184
        $this->getPdo()
185
            ->exec($sql);
186
        $columnDefaults = [
187
            'ownerID' => $ownerID,
188
            'accountKey' => null,
189
            'description' => null
190
        ];
191
        $xPath = '//walletDivisions/row';
192
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
193
        $this->attributePreserveData($elements, $columnDefaults, $tableName);
194
    }
195
    /**
196
     * Special override to work around bug in Eve API server when including both KeyID and corporationID.
197
     *
198
     * @param EveApiReadWriteInterface $data
199
     * @param MediatorInterface        $yem
200
     *
201
     * @return bool
202
     * @throws \DomainException
203
     * @throws \InvalidArgumentException
204
     * @throws \LogicException
205
     * @throws \UnexpectedValueException
206
     */
207
    protected function processEvents(EveApiReadWriteInterface $data, MediatorInterface $yem): bool
208
    {
209
        $corpID = 0;
210
        $eventSuffixes = ['retrieve', 'transform', 'validate', 'preserve'];
211
        foreach ($eventSuffixes as $eventSuffix) {
212
            if ('retrieve' === $eventSuffix) {
213
                $corp = $data->getEveApiArguments();
214
                $corpID = $corp['corporationID'];
215
                // Can NOT include corporationID or will only get public info.
216
                if (array_key_exists('keyID', $corp)) {
217
                    unset($corp['corporationID']);
218
                    $data->setEveApiArguments($corp);
219
                }
220
            }
221
            if (false === $this->emitEvents($data, $eventSuffix)) {
222
                return false;
223
            }
224
            if ('retrieve' === $eventSuffix) {
225
                $data->addEveApiArgument('corporationID', (string)$corpID);
226
            }
227
            if (false === $data->getEveApiXml()) {
228
                if ($data->hasEveApiArgument('accountKey') && '10000' === $data->getEveApiArgument('accountKey')
229
                    && 'corp' === strtolower($data->getEveApiSectionName())
230
                ) {
231
                    $mess = 'No faction warfare account data in';
232
                    $yem->triggerLogEvent('Yapeal.Log.log', Logger::INFO, $this->createEveApiMessage($mess, $data));
233
                    return false;
234
                }
235
                $yem->triggerLogEvent('Yapeal.Log.log',
236
                    Logger::INFO,
237
                    $this->getEmptyXmlDataMessage($data, $eventSuffix));
238
                return false;
239
            }
240
        }
241
        return true;
242
    }
243
}
244