StarbaseDetail::preserveToStarbaseDetail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 22
cp 0
rs 9.2
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains class StarbaseDetail.
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\Event\EveApiEventInterface;
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 StarbaseDetail.
46
 */
47
class StarbaseDetail extends CorpSection implements EveApiPreserverInterface
48
{
49
    use PreserverTrait;
50
51
    /**
52
     * Constructor
53
     */
54
    public function __construct()
55
    {
56
        $this->mask = 131072;
57
        $this->preserveTos = [
58
            'preserveToCombatSettings',
59
            'preserveToFuel',
60
            'preserveToGeneralSettings',
61
            'preserveToStarbaseDetail'
62
        ];
63
    }
64
    /**
65
     * @param EveApiEventInterface $event
66
     * @param string               $eventName
67
     * @param MediatorInterface    $yem
68
     *
69
     * @return EveApiEventInterface
70
     * @throws \DomainException
71
     * @throws \InvalidArgumentException
72
     * @throws \LogicException
73
     * @throws \Yapeal\Exception\YapealDatabaseException
74
     */
75
    public function startEveApi(EveApiEventInterface $event, string $eventName, MediatorInterface $yem)
76
    {
77
        if (!$this->hasYem()) {
78
            $this->setYem($yem);
79
        }
80
        $data = $event->getData();
81
        $apiName = $data->getEveApiName();
82
        $data->setEveApiName('StarbaseList');
83
        // Insure Starbase list has already been updated first so we have current list to get details with.
84
        $this->emitEvents($data, 'start');
85
        $data->setEveApiName($apiName)
86
            ->setEveApiXml('');
87
        return parent::startEveApi($event->setData($data), $eventName, $yem);
88
    }
89
    /**
90
     * @param EveApiReadWriteInterface $data
91
     *
92
     * @return void
93
     * @throws \LogicException
94
     */
95
    protected function preserveToCombatSettings(EveApiReadWriteInterface $data)
96
    {
97
        $tableName = 'corpCombatSettings';
98
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
99
        $starbaseID = (int)$data->getEveApiArgument('itemID');
100
        $sql = $this->getCsq()
101
            ->getDeleteFromStarbaseDetailTables($tableName, $ownerID, $starbaseID);
102
        $this->getYem()
103
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
104
        $this->getPdo()
105
            ->exec($sql);
106
        $columnDefaults = [
107
            'itemID' => $starbaseID,
108
            'onAggressionEnabled' => '0',
109
            'onCorporationWarEnabled' => '0',
110
            'onStandingDropStanding' => '0',
111
            'onStatusDropEnabled' => '0',
112
            'onStatusDropStanding' => '0',
113
            'ownerID' => $ownerID,
114
            'useStandingsFromOwnerID' => '0'
115
        ];
116
        $xPath = '//combatSettings/row';
117
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
118
        $this->attributePreserveData($elements, $columnDefaults, $tableName);
119
    }
120
    /**
121
     * @param EveApiReadWriteInterface $data
122
     *
123
     * @return void
124
     * @throws \LogicException
125
     */
126
    protected function preserveToFuel(EveApiReadWriteInterface $data)
127
    {
128
        $tableName = 'corpFuel';
129
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
130
        $starbaseID = (int)$data->getEveApiArgument('itemID');
131
        $sql = $this->getCsq()
132
            ->getDeleteFromStarbaseDetailTables($tableName, $ownerID, $starbaseID);
133
        $this->getYem()
134
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
135
        $this->getPdo()
136
            ->exec($sql);
137
        $columnDefaults = [
138
            'itemID' => $starbaseID,
139
            'ownerID' => $ownerID,
140
            'quantity' => '0',
141
            'typeID' => '0'
142
        ];
143
        $xPath = '//fuel/row';
144
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
145
        $this->attributePreserveData($elements, $columnDefaults, $tableName);
146
    }
147
    /**
148
     * @param EveApiReadWriteInterface $data
149
     *
150
     * @return void
151
     * @throws \LogicException
152
     */
153
    protected function preserveToGeneralSettings(EveApiReadWriteInterface $data)
154
    {
155
        $tableName = 'corpGeneralSettings';
156
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
157
        $starbaseID = (int)$data->getEveApiArgument('itemID');
158
        $sql = $this->getCsq()
159
            ->getDeleteFromStarbaseDetailTables($tableName, $ownerID, $starbaseID);
160
        $this->getYem()
161
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
162
        $this->getPdo()
163
            ->exec($sql);
164
        $columnDefaults = [
165
            'allowAllianceMembers' => '0',
166
            'allowCorporationMembers' => '0',
167
            'deployFlags' => '0',
168
            'itemID' => $starbaseID,
169
            'ownerID' => $ownerID,
170
            'usageFlags' => '0'
171
        ];
172
        $xPath = '//generalSettings/row';
173
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
174
        $this->attributePreserveData($elements, $columnDefaults, $tableName);
175
    }
176
    /**
177
     * @param EveApiReadWriteInterface $data
178
     *
179
     * @return void
180
     * @throws \LogicException
181
     */
182
    protected function preserveToStarbaseDetail(EveApiReadWriteInterface $data)
183
    {
184
        $tableName = 'corpStarbaseDetail';
185
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
186
        $starbaseID = (int)$data->getEveApiArgument('itemID');
187
        $sql = $this->getCsq()
188
            ->getDeleteFromStarbaseDetailTables($tableName, $ownerID, $starbaseID);
189
        $this->getYem()
190
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
191
        $this->getPdo()
192
            ->exec($sql);
193
        $columnDefaults = [
194
            'ownerID' => $ownerID,
195
            'itemID' => $starbaseID,
196
            'onlineTimestamp' => '1970-01-01 00:00:01',
197
            'state' => '0',
198
            'stateTimestamp' => '1970-01-01 00:00:01'
199
        ];
200
        $xPath = '//result/child::*[not(*|@*|self::dataTime)]';
201
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
202
        $this->valuesPreserveData($elements, $columnDefaults, $tableName);
203
    }
204
}
205