Completed
Push — master ( 2cff5f...81de03 )
by Michael
03:19
created

Validator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 11
Bugs 3 Features 2
Metric Value
wmc 11
c 11
b 3
f 2
lcom 2
cbo 5
dl 0
loc 95
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validateEveApi() 0 58 8
A getDom() 0 7 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 Yapeal\Event\EveApiEventEmitterTrait;
37
use Yapeal\Event\EveApiEventInterface;
38
use Yapeal\Event\MediatorInterface;
39
use Yapeal\FileSystem\RelativeFileSearchTrait;
40
use Yapeal\Log\Logger;
41
42
/**
43
 * Class Validator
44
 */
45
class Validator
46
{
47
    use EveApiEventEmitterTrait, RelativeFileSearchTrait;
48
    /**
49
     * Constructor.
50
     *
51
     * @param string $dir Base directory where Eve API XSD files can be found.
52
     */
53
    public function __construct($dir = __DIR__)
54
    {
55
        $this->setRelativeBaseDir($dir . '/');
56
    }
57
    /**
58
     * @param EveApiEventInterface $event
59
     * @param string               $eventName
60
     * @param MediatorInterface    $yem
61
     *
62
     * @return EveApiEventInterface
63
     * @throws \DomainException
64
     * @throws \InvalidArgumentException
65
     * @throws \LogicException
66
     */
67
    public function validateEveApi(EveApiEventInterface $event, $eventName, MediatorInterface $yem)
68
    {
69
        $this->setYem($yem);
70
        $data = $event->getData();
71
        $yem->triggerLogEvent('Yapeal.Log.log',
72
            Logger::DEBUG,
73
            $this->getReceivedEventMessage($data, $eventName, __CLASS__));
74
        $htmlError = strpos($data->getEveApiXml(), '<!DOCTYPE html');
75
        if (false !== $htmlError) {
76
            $mess = 'Received HTML result from ';
77
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($mess, $data));
78
            $apiName = $data->getEveApiName();
79
            $data->setEveApiName('Invalid_' . $apiName);
80
            // Cache error html.
81
            $this->emitEvents($data, 'preserve', 'Yapeal.Xml.Error');
82
            $data->setEveApiName($apiName);
83
            return $event->setData($data);
84
        }
85
        $fileName = $this->findEveApiFile($data->getEveApiSectionName(), $data->getEveApiName(), 'xsd');
86
        if ('' === $fileName) {
87
            return $event;
88
        }
89
        libxml_clear_errors();
90
        libxml_use_internal_errors(true);
91
        libxml_clear_errors();
92
        $dom = new \DOMDocument();
93
        $loaded = $dom->loadXML($data->getEveApiXml());
94
        if (!$loaded || !$dom->schemaValidate($fileName)) {
95
            /**
96
             * @var array $errors
97
             */
98
            $errors = libxml_get_errors();
99
            if (0 !== count($errors)) {
100
                foreach ($errors as $error) {
101
                    $this->getYem()
102
                        ->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $error->message);
103
                }
104
            }
105
            libxml_clear_errors();
106
            libxml_use_internal_errors(false);
107
            libxml_clear_errors();
108
            $apiName = $data->getEveApiName();
109
            $data->setEveApiName('Invalid_' . $apiName);
110
            // Cache error causing XML.
111
            $this->emitEvents($data, 'preserve', 'Yapeal.Xml.Error');
112
            $data->setEveApiName($apiName);
113
            return $event->setData($data);
114
        }
115
        libxml_clear_errors();
116
        libxml_use_internal_errors(false);
117
        libxml_clear_errors();
118
        // Check for XML error element.
119
        if (false !== strpos($data->getEveApiXml(), '<error ')) {
120
            $this->emitEvents($data, 'start', 'Yapeal.Xml.Error');
121
            return $event->setData($data);
122
        }
123
        return $event->setHandledSufficiently();
124
    }
125
    /**
126
     * @return \DOMDocument
127
     */
128
    private function getDom()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
129
    {
130
        if (null === $this->getDom()) {
131
            $this->dom = new \DOMDocument();
132
        }
133
        return $this->dom;
134
    }
135
    /**
136
     * @var \DOMDocument $dom
137
     */
138
    private $dom;
139
}
140