• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / xmltv-library / 3821848929

pending completion
3821848929

push

github

webeweb
Remove unused configuration files

1302 of 1306 relevant lines covered (99.69%)

294.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/src/Provider/XmlProvider.php
1
<?php
2

3
/*
4
 * This file is part of the xmltv-library package.
5
 *
6
 * (c) 2020 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WBW\Library\XmlTv\Provider;
13

14
use DOMDocument;
15
use GuzzleHttp\Client;
16
use GuzzleHttp\Exception\GuzzleException;
17
use Psr\Log\LoggerInterface;
18
use RuntimeException;
19
use WBW\Library\Provider\Helper\GuzzleHelper;
20
use WBW\Library\XmlTv\Model\Tv;
21
use WBW\Library\XmlTv\Serializer\SerializerHelper;
22
use WBW\Library\XmlTv\Serializer\XmlDeserializer;
23
use WBW\Library\XmlTv\Statistic\Statistic;
24
use WBW\Library\XmlTv\Statistic\Statistics;
25

26
/**
27
 * XML provider.
28
 *
29
 * @author webeweb <https://github.com/webeweb>
30
 * @package WBW\Library\XmlTv\Provider
31
 */
32
class XmlProvider {
33

34
    /**
35
     * Get the DTD.
36
     *
37
     * @return string Returns the DTD.
38
     */
39
    public static function getDtd(): string {
40
        return realpath(__DIR__ . "/../Resources/config/xmltv.dtd");
21✔
41
    }
42

43
    /**
44
     * Get an XML file.
45
     *
46
     * @param string $url The URL.
47
     * @param string $filename The filename.
48
     * @param LoggerInterface|null $logger The logger.
49
     * @return Tv Returns the TV.
50
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
51
     * @throws Throwable Throws an exception if an error occurs.
52
     */
53
    public static function getXml(string $url, string $filename, LoggerInterface $logger = null): Tv {
54

55
        $saveTo = GuzzleHelper::getStreamParameterName();
7✔
56
        $stream = fopen($filename, "w");
7✔
57

58
        $client = new Client([
7✔
59
            "headers"     => [
7✔
60
                "Accept"     => "text/xml",
5✔
61
                "User-Agent" => "webeweb/xmltv-library",
5✔
62
            ],
5✔
63
            $saveTo       => $stream,
7✔
64
            "synchronous" => true,
5✔
65
        ]);
5✔
66

67
        $client->request("GET", $url);
7✔
68

69
        return static::readXml($filename, $logger);
7✔
70
    }
71

72
    /**
73
     * Read an XML file.
74
     *
75
     * @param string $filename The filename.
76
     * @param LoggerInterface|null $logger The logger.
77
     * @return Tv Returns the TV.
78
     * @throws RuntimeException Throws a runtime exception if an error occurs.
79
     */
80
    public static function readXml(string $filename, LoggerInterface $logger = null): Tv {
81

82
        SerializerHelper::setLogger($logger);
21✔
83

84
        $document = new DOMDocument();
21✔
85
        if (false === @$document->load($filename)) {
21✔
86
            throw new RuntimeException(libxml_get_last_error()->message, 500);
×
87
        }
88
        if (false === @$document->schemaValidate(static::getDtd()) && null !== $logger) {
21✔
89
            $logger->warning("Schema validation failed", ["_filename" => $filename]);
7✔
90
        }
91

92
        return XmlDeserializer::deserializeTv($document->documentElement);
21✔
93
    }
94

95
    /**
96
     * Stat an XML file.
97
     *
98
     * @param string $filename The filename.
99
     * @return Statistic[] Returns the statistics.
100
     * @throws RuntimeException Throws a runtime exception if an error occurs.
101
     */
102
    public static function statXml(string $filename): array {
103

104
        $document = new DOMDocument();
7✔
105
        if (false === @$document->load($filename)) {
7✔
106
            throw new RuntimeException(libxml_get_last_error()->message, 500);
×
107
        }
108

109
        $statistics = new Statistics();
7✔
110
        $statistics->parse($document->documentElement);
7✔
111

112
        return $statistics->getStatistics();
7✔
113
    }
114

115
    /**
116
     * Write an XML file.
117
     *
118
     * @param Tv $tv The TV.
119
     * @param string $filename The filename.
120
     * @param LoggerInterface|null $logger The logger.
121
     * @return int Returns the number of bytes written.
122
     */
123
    public static function writeXml(Tv $tv, string $filename, LoggerInterface $logger = null): int {
124

125
        $xml = [
5✔
126
            '<?xml version="1.0" encoding="utf-8"?>',
7✔
127
            '<!DOCTYPE tv SYSTEM "xmltv.dtd">',
7✔
128
            $tv->xmlSerialize(),
7✔
129
        ];
5✔
130

131
        $document = new DOMDocument();
7✔
132
        $document->loadXML(implode("", $xml));
7✔
133
        if (false === @$document->schemaValidate(static::getDtd()) && null !== $logger) {
7✔
134
            $logger->warning("Schema validation failed");
7✔
135
        }
136

137
        $document->formatOutput = true;
7✔
138

139
        return $document->save($filename);
7✔
140
    }
141
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc