Completed
Push — master ( 7c429e...75748b )
by Michael
03:23
created

ErrorCacheIntervalSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 64
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C processXmlError() 0 50 7
1
<?php
2
/**
3
 * Contains class ErrorCacheIntervalSubscriber.
4
 *
5
 * PHP version 5.5
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   LGPL-3.0+
32
 * @author    Michael Cummings <[email protected]>
33
 */
34
namespace Yapeal\Xml;
35
36
use Yapeal\Event\EveApiEventEmitterTrait;
37
use Yapeal\Event\EveApiEventInterface;
38
use Yapeal\Event\MediatorInterface;
39
use Yapeal\Log\Logger;
40
41
/**
42
 * Class ErrorCacheIntervalSubscriber.
43
 */
44
class ErrorCacheIntervalSubscriber
45
{
46
    use EveApiEventEmitterTrait;
47
    /**
48
     * @param EveApiEventInterface $event
49
     * @param string               $eventName
50
     * @param MediatorInterface    $yem
51
     *
52
     * @return EveApiEventInterface
53
     * @throws \DomainException
54
     * @throws \InvalidArgumentException
55
     * @throws \LogicException
56
     */
57
    protected function processXmlError(EveApiEventInterface $event, $eventName, MediatorInterface $yem)
58
    {
59
        $this->setYem($yem);
60
        $data = $event->getData();
61
        $this->getYem()
62
             ->triggerLogEvent(
63
                 'Yapeal.Log.log',
64
                 Logger::DEBUG,
65
                 $this->getReceivedEventMessage($data, $eventName, __CLASS__)
66
             );
67
        $simple = new \SimpleXMLElement($data->getEveApiXml());
68
        /** @noinspection PhpUndefinedFieldInspection */
69
        $errorText = (string)$simple->error[0];
70
        /** @noinspection PhpUndefinedFieldInspection */
71
        if (isset($simple->error[0]['code'])) {
72
            /** @noinspection PhpUndefinedFieldInspection */
73
            $code = (int)$simple->error[0]['code'];
74
            $mess = sprintf('Received XML error (%1$s) - %2$s during', $code, $errorText);
75
        } else {
76
            $mess = sprintf('Received XML error with no code attribute - %1$s during', $errorText);
77
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::WARNING,
78
                $this->createEventMessage($mess, $data, $eventName));
79
            return $event;
80
        }
81
        if ($code < 200) {
82
            if (false !== strpos($mess, 'retry after')) {
83
                $data->setCacheInterval(strtotime(substr($mess, -19) . '+00:00') - time());
84
            }
85
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::WARNING,
86
                $this->createEventMessage($mess, $data, $eventName));
87
        } elseif ($code < 300) {
88
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::ERROR, $this->createEventMessage($mess, $data, $eventName));
89
            $data->setCacheInterval(86400);
90
        } elseif ($code > 903 && $code < 905) {
91
            // Major application or Yapeal error.
92
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::ALERT, $this->createEventMessage($mess, $data, $eventName));
93
            $data->setCacheInterval(86400);
94
        } else {
95
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::WARNING,
96
                $this->createEventMessage($mess, $data, $eventName));
97
            $data->setCacheInterval(300);
98
        }
99
        $apiName = $data->getEveApiName();
100
        $data->setEveApiName('Error_' . $apiName);
101
        // Cache error XML.
102
        $this->emitEvents($data, 'cache');
103
        $data->setEveApiName($apiName);
104
        return $event->setData($data)
105
                     ->setHandledSufficiently();
106
    }
107
}
108