IndustryJobs::preserveToIndustryJobs()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 46
ccs 0
cts 46
cp 0
rs 8.9411
cc 1
eloc 43
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains class IndustryJobs.
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
 * @copyright 2016-2017 Michael Cummings
32
 * @license   LGPL-3.0+
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\EveApi\Char;
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 IndustryJobs
46
 */
47
class IndustryJobs extends CharSection implements EveApiPreserverInterface
48
{
49
    use PreserverTrait;
50
51
    /**
52
     * Constructor
53
     */
54
    public function __construct()
55
    {
56
        $this->mask = 128;
57
        $this->preserveTos = [
58
            'preserveToIndustryJobs'
59
        ];
60
    }
61
    /**
62
     * @param EveApiEventInterface $event
63
     * @param string               $eventName
64
     * @param MediatorInterface    $yem
65
     *
66
     * @return EveApiEventInterface
67
     * @throws \DomainException
68
     * @throws \InvalidArgumentException
69
     * @throws \LogicException
70
     * @throws \Yapeal\Exception\YapealDatabaseException
71
     */
72
    public function startEveApi(EveApiEventInterface $event, string $eventName, MediatorInterface $yem)
73
    {
74
        if (!$this->hasYem()) {
75
            $this->setYem($yem);
76
        }
77
        $data = $event->getData();
78
        $apiName = $data->getEveApiName();
79
        $data->setEveApiName($apiName . 'History');
80
        // Insure history has already been updated first so current data overwrites old data.
81
        $this->emitEvents($data, 'start');
82
        $data->setEveApiName($apiName)
83
            ->setEveApiXml('');
84
        return parent::startEveApi($event->setData($data), $eventName, $yem);
85
    }
86
    /**
87
     * @param EveApiReadWriteInterface $data
88
     *
89
     * @return self Fluent interface.
90
     * @throws \LogicException
91
     */
92
    protected function preserveToIndustryJobs(EveApiReadWriteInterface $data)
93
    {
94
        $tableName = 'charIndustryJobs';
95
        $ownerID = $this->extractOwnerID($data->getEveApiArguments());
96
        $sql = $this->getCsq()
97
            ->getDeleteFromTableWithOwnerID($tableName, $ownerID);
98
        $this->getYem()
99
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
100
        $this->getPdo()
101
            ->exec($sql);
102
        $columnDefaults = [
103
            'activityID' => null,
104
            'blueprintID' => null,
105
            'blueprintLocationID' => null,
106
            'blueprintTypeID' => null,
107
            'blueprintTypeName' => '',
108
            'completedCharacterID' => null,
109
            'completedDate' => null,
110
            'cost' => null,
111
            'endDate' => null,
112
            'facilityID' => null,
113
            'installerID' => null,
114
            'installerName' => '',
115
            'jobID' => null,
116
            'licensedRuns' => null,
117
            'outputLocationID' => null,
118
            'ownerID' => $ownerID,
119
            'pauseDate' => null,
120
            'probability' => null,
121
            'productTypeID' => null,
122
            'productTypeName' => '',
123
            'runs' => null,
124
            'solarSystemID' => null,
125
            'solarSystemName' => '',
126
            'startDate' => null,
127
            'stationID' => null,
128
            'status' => null,
129
            'successfulRuns' => null,
130
            'teamID' => null,
131
            'timeInSeconds' => null
132
        ];
133
        $xPath = '//jobs/row';
134
        $elements = (new \SimpleXMLElement($data->getEveApiXml()))->xpath($xPath);
135
        $this->attributePreserveData($elements, $columnDefaults, $tableName);
136
        return $this;
137
    }
138
}
139