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

Validator::processEveApiXmlError()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 32
rs 6.7272
cc 7
eloc 21
nc 6
nop 2
1
<?php
2
/**
3
 * Contains Validator class.
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) 2015-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 2015-2016 Michael Cummings
31
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
32
 * @author    Michael Cummings <[email protected]>
33
 */
34
namespace Yapeal\Xsd;
35
36
use DOMDocument;
37
use Yapeal\Event\EveApiEventEmitterTrait;
38
use Yapeal\Event\EveApiEventInterface;
39
use Yapeal\Event\MediatorInterface;
40
use Yapeal\FileSystem\RelativeFileSearchTrait;
41
use Yapeal\Log\Logger;
42
43
/**
44
 * Class Validator
45
 */
46
class Validator
47
{
48
    use EveApiEventEmitterTrait, RelativeFileSearchTrait;
49
    /**
50
     * Constructor.
51
     *
52
     * @param string $dir Base directory where Eve API XSD files can be found.
53
     */
54
    public function __construct($dir = __DIR__)
55
    {
56
        $this->setRelativeBaseDir($dir . '/');
57
    }
58
    /**
59
     * @param EveApiEventInterface $event
60
     * @param string               $eventName
61
     * @param MediatorInterface    $yem
62
     *
63
     * @return EveApiEventInterface
64
     * @throws \DomainException
65
     * @throws \InvalidArgumentException
66
     * @throws \LogicException
67
     */
68
    public function validateEveApi(EveApiEventInterface $event, $eventName, MediatorInterface $yem)
69
    {
70
        $this->setYem($yem);
71
        $data = $event->getData();
72
        $this->getYem()
73
             ->triggerLogEvent(
74
                 'Yapeal.Log.log',
75
                 Logger::DEBUG,
76
                 $this->getReceivedEventMessage($data, $eventName, __CLASS__)
77
             );
78
        $fileName = $this->findEveApiFile($data->getEveApiSectionName(), $data->getEveApiName(), 'xsd');
79
        if ('' === $fileName) {
80
            return $event;
81
        }
82
        $oldErrors = libxml_use_internal_errors(true);
83
        libxml_clear_errors();
84
        $dom = new DOMDocument();
85
        $dom->loadXML($data->getEveApiXml());
86
        if (!$dom->schemaValidate($fileName)) {
87
            /**
88
             * @var array $errors
89
             */
90
            $errors = libxml_get_errors();
91
            if (0 !== count($errors)) {
92
                foreach ($errors as $error) {
93
                    $this->getYem()
94
                         ->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $error->message);
95
                }
96
            }
97
            libxml_clear_errors();
98
            libxml_use_internal_errors($oldErrors);
99
            return $event;
100
        }
101
        libxml_clear_errors();
102
        libxml_use_internal_errors($oldErrors);
103
        // Check for XML error element.
104
        if (false !== strpos($data->getEveApiXml(), '<error ')) {
105
            $this->emitEvents($data, 'error', 'Yapeal.Xml');
106
            return $event;
107
        }
108
        return $event->eventHandled();
109
    }
110
}
111