Completed
Push — master ( 75748b...36048b )
by Michael
06:58
created

APIKeyInfo::preserveToAPIKeyInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
/**
3
 * Contains APIKeyInfo class.
4
 *
5
 * PHP version 5.4
6
 *
7
 * LICENSE:
8
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
9
 * which can be used to access the Eve Online API data and place it into a
10
 * database.
11
 * Copyright (C) 2016 Michael Cummings
12
 *
13
 * This program is free software: you can redistribute it and/or modify it
14
 * under the terms of the GNU Lesser General Public License as published by the
15
 * Free Software Foundation, either version 3 of the License, or (at your
16
 * option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful, but WITHOUT
19
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
21
 * for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public License
24
 * along with this program. If not, see
25
 * <http://www.gnu.org/licenses/>.
26
 *
27
 * You should be able to find a copy of this license in the LICENSE.md file. A
28
 * copy of the GNU GPL should also be available in the GNU-GPL.md file.
29
 *
30
 * @copyright 2016 Michael Cummings
31
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
32
 * @author    Michael Cummings <[email protected]>
33
 */
34
namespace Yapeal\EveApi\Account;
35
36
use PDOException;
37
use Yapeal\EveApi\ActiveTrait;
38
use Yapeal\Event\EveApiEventInterface;
39
use Yapeal\Event\MediatorInterface;
40
use Yapeal\Log\Logger;
41
use Yapeal\Sql\PreserverTrait;
42
43
/**
44
 * Class APIKeyInfo
45
 */
46
class APIKeyInfo extends AccountSection
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: getCsq, getPdo
Loading history...
47
{
48
    use ActiveTrait, PreserverTrait;
49
    /**
50
     * @param EveApiEventInterface $event
51
     * @param string               $eventName
52
     * @param MediatorInterface    $yem
53
     *
54
     * @return EveApiEventInterface
55
     * @throws \DomainException
56
     * @throws \InvalidArgumentException
57
     * @throws \LogicException
58
     */
59
    public function preserveEveApi(EveApiEventInterface $event, $eventName, MediatorInterface $yem)
60
    {
61
        $this->setYem($yem);
62
        $data = $event->getData();
63
        $xml = $data->getEveApiXml();
64
        if (false === $xml) {
65
            return $event->setHandledSufficiently();
66
        }
67
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
68
        $this->getYem()
69
             ->triggerLogEvent(
70
                 'Yapeal.Log.log',
71
                 Logger::DEBUG,
72
                 $this->getReceivedEventMessage($data, $eventName, __CLASS__)
73
             );
74
        $this->getPdo()
75
             ->beginTransaction();
76
        try {
77
            $this->preserveToAPIKeyInfo($xml, $ownerID)
78
                 ->preserveToCharacters($xml)
79
                 ->preserveToKeyBridge($xml, $ownerID);
80
            $this->getPdo()
81
                 ->commit();
82
        } catch (PDOException $exc) {
83
            $mess = 'Failed to upsert data of';
84
            $this->getYem()
85
                 ->triggerLogEvent(
86
                     'Yapeal.Log.log',
87
                     Logger::WARNING,
88
                     $this->createEveApiMessage($mess, $data),
89
                     ['exception' => $exc]
90
                 );
91
            $this->getPdo()
92
                 ->rollBack();
93
            return $event;
94
        }
95
        return $event->setHandledSufficiently();
96
    }
97
    /**
98
     * @param string $xml
99
     * @param string $ownerID
100
     *
101
     * @return self Fluent interface.
102
     * @throws \DomainException
103
     * @throws \InvalidArgumentException
104
     * @throws \LogicException
105
     */
106
    protected function preserveToAPIKeyInfo($xml, $ownerID)
107
    {
108
        $tableName = 'accountAPIKeyInfo';
109
        $columnDefaults = [
110
            'accessMask' => null,
111
            'expires' => '2038-01-19 03:14:07',
112
            'keyID' => $ownerID,
113
            'type' => null
114
        ];
115
        $this->attributePreserveData($xml, $columnDefaults, $tableName, '//key');
116
        return $this;
117
    }
118
    /**
119
     * @param string $xml
120
     *
121
     * @return self Fluent interface.
122
     * @throws \DomainException
123
     * @throws \InvalidArgumentException
124
     * @throws \LogicException
125
     */
126
    protected function preserveToCharacters($xml)
127
    {
128
        $tableName = 'accountCharacters';
129
        $columnDefaults = [
130
            'allianceID' => null,
131
            'allianceName' => null,
132
            'characterID' => null,
133
            'characterName' => null,
134
            'corporationID' => null,
135
            'corporationName' => null,
136
            'factionID' => null,
137
            'factionName' => null
138
        ];
139
        $this->attributePreserveData($xml, $columnDefaults, $tableName, '//characters/row');
140
        return $this;
141
    }
142
    /** @noinspection PhpMissingParentCallCommonInspection */
143
    /**
144
     * @param string $xml
145
     * @param string $ownerID
146
     *
147
     * @return self Fluent interface.
148
     * @throws \DomainException
149
     * @throws \InvalidArgumentException
150
     * @throws \LogicException
151
     */
152
    protected function preserveToKeyBridge($xml, $ownerID)
153
    {
154
        $tableName = 'accountKeyBridge';
155
        $columnDefaults = ['keyID' => $ownerID, 'characterID' => null];
156
        $this->attributePreserveData($xml, $columnDefaults, $tableName, '//characters/row');
157
        return $this;
158
    }
159
}
160