LibXmlChecksTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 59
ccs 21
cts 27
cp 0.7778
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C checkLibXmlErrors() 0 37 7
createEveApiMessage() 0 1 ?
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains trait LibXmlChecksTrait.
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
 * @author    Michael Cummings <[email protected]>
32
 * @copyright 2016-2017 Michael Cummings
33
 * @license   LGPL-3.0+
34
 */
35
namespace Yapeal\Xml;
36
37
use Yapeal\Event\MediatorInterface;
38
use Yapeal\Log\Logger;
39
40
/**
41
 * Trait LibXmlChecksTrait.
42
 */
43
trait LibXmlChecksTrait
44
{
45
    /**
46
     * Checks for any libxml errors and logs them.
47
     *
48
     * @param EveApiReadWriteInterface $data
49
     * @param MediatorInterface        $yem
50
     *
51
     * @throws \DomainException
52
     * @throws \InvalidArgumentException
53
     * @throws \LogicException
54
     * @throws \UnexpectedValueException
55
     */
56 7
    protected function checkLibXmlErrors(EveApiReadWriteInterface $data, MediatorInterface $yem)
57
    {
58
        /**
59
         * @var \libXMLError[] $errors
60
         */
61 7
        $errors = libxml_get_errors();
62 7
        if (0 !== count($errors)) {
63 7
            foreach ($errors as $error) {
64 7
                switch ($error->level) {
65 7
                    case LIBXML_ERR_WARNING:
66
                        $messagePrefix = 'Libxml Warning';
67
                        $level = Logger::NOTICE;
68
                        break;
69 7
                    case LIBXML_ERR_ERROR:
70 2
                        $messagePrefix = 'Libxml Error';
71 2
                        $level = Logger::WARNING;
72 2
                        break;
73 5
                    case LIBXML_ERR_FATAL:
74 5
                        $messagePrefix = 'Libxml Fatal Error';
75 5
                        $level = Logger::ERROR;
76 5
                        break;
77
                    default:
78
                        $messagePrefix = '';
79
                        $level = Logger::DEBUG;
80
                        break;
81
                }
82 7
                if ('' !== $messagePrefix) {
83 7
                    $messagePrefix .= sprintf(' %u: %s - Col: %u Line: %u during the transform of',
84 7
                        $error->code,
85 7
                        trim($error->message),
86 7
                        $error->column,
87 7
                        $error->line);
88 7
                    $yem->triggerLogEvent('Yapeal.Log.log', $level, $this->createEveApiMessage($messagePrefix, $data));
89
                }
90
            }
91
        }
92
    }
93
    /**
94
     * @param string                   $messagePrefix
95
     * @param EveApiReadWriteInterface $data
96
     *
97
     * @return string
98
     * @throws \LogicException
99
     */
100
    abstract protected function createEveApiMessage(string $messagePrefix, EveApiReadWriteInterface $data): string;
101
}
102