ErrorCacheIntervalSubscriber::processXmlError()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 47
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 0
cts 33
cp 0
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 40
nc 5
nop 3
crap 42
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains class ErrorCacheIntervalSubscriber.
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\Xml;
36
37
use Yapeal\Event\EveApiEventEmitterTrait;
38
use Yapeal\Event\EveApiEventInterface;
39
use Yapeal\Event\MediatorInterface;
40
use Yapeal\Log\Logger;
41
42
/**
43
 * Class ErrorCacheIntervalSubscriber.
44
 */
45
class ErrorCacheIntervalSubscriber
46
{
47
    use EveApiEventEmitterTrait;
48
    /**
49
     * @param EveApiEventInterface $event
50
     * @param string               $eventName
51
     * @param MediatorInterface    $yem
52
     *
53
     * @return EveApiEventInterface
54
     * @throws \DomainException
55
     * @throws \InvalidArgumentException
56
     * @throws \LogicException
57
     * @throws \UnexpectedValueException
58
     */
59
    public function processXmlError(
60
        EveApiEventInterface $event,
61
        string $eventName,
62
        MediatorInterface $yem
63
    ): EveApiEventInterface {
64
        $this->setYem($yem);
65
        $data = $event->getData();
66
        $yem->triggerLogEvent('Yapeal.Log.log',
67
            Logger::DEBUG,
68
            $this->getReceivedEventMessage($data, $eventName, __CLASS__));
69
        $simple = new \SimpleXMLElement($data->getEveApiXml());
70
        /** @noinspection PhpUndefinedFieldInspection */
71
        $errorText = (string)$simple->error[0];
72
        /** @noinspection PhpUndefinedFieldInspection */
73
        $code = (int)$simple->error[0]['code'] ?? 0;
74
        $mess = sprintf('Received from the Eve API server an XML error (%s) "%s" response during the validation of',
75
            $code,
76
            $errorText);
77
        $mess = $this->createEveApiMessage($mess, $data);
78
        if ($code < 200) {
79
            if (false !== strpos($errorText, 'retry after')) {
80
                $data->setCacheInterval(strtotime(substr($errorText, -19) . '+00:00') - time());
81
            }
82
            $yem->triggerLogEvent('Yapeal.Log.log',
83
                Logger::WARNING,
84
                $this->createEventMessage($mess, $data, $eventName));
85
        } elseif ($code < 300) {
86
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::ERROR, $this->createEventMessage($mess, $data, $eventName));
87
            $data->setCacheInterval(86400);
88
        } elseif ($code > 903 && $code < 905) {
89
            // Major application or Yapeal error.
90
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::ALERT, $this->createEventMessage($mess, $data, $eventName));
91
            $data->setCacheInterval(86400);
92
        } else {
93
            $yem->triggerLogEvent('Yapeal.Log.log',
94
                Logger::WARNING,
95
                $this->createEventMessage($mess, $data, $eventName));
96
            $data->setCacheInterval(300);
97
        }
98
        // Cache error XML.
99
        $apiName = $data->getEveApiName();
100
        $data->setEveApiName('Error_' . $apiName);
101
        $this->emitEvents($data, 'preserve', 'Yapeal.Xml.Error');
102
        $data->setEveApiName($apiName);
103
        return $event->setData($data)
104
            ->setHandledSufficiently();
105
    }
106
}
107