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

webeweb / xmltv-library / 8876587662

29 Apr 2024 09:48AM UTC coverage: 99.723%. Remained the same
8876587662

push

github

webeweb
Update CHANGELOG

1441 of 1445 relevant lines covered (99.72%)

311.43 hits per line

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

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

3
declare(strict_types = 1);
4

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

14
namespace WBW\Library\XmlTv\Provider;
15

16
use DOMDocument;
17
use GuzzleHttp\Client;
18
use Psr\Log\LoggerInterface;
19
use RuntimeException;
20
use Throwable;
21
use WBW\Library\Common\Provider\GuzzleHelper;
22
use WBW\Library\XmlTv\Model\Tv;
23
use WBW\Library\XmlTv\Serializer\XmlDeserializer;
24
use WBW\Library\XmlTv\Statistic\Statistic;
25
use WBW\Library\XmlTv\Statistic\Statistics;
26

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

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

44
    /**
45
     * Get an XML file.
46
     *
47
     * @param string $url The URL.
48
     * @param string $filename The filename.
49
     * @param LoggerInterface|null $logger The logger.
50
     * @return Tv Returns the TV.
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();
8✔
56
        $stream = fopen($filename, "w");
8✔
57

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

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

69
        return static::readXml($filename, $logger);
8✔
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
        XmlDeserializer::setLogger($logger);
24✔
83

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

92
        return XmlDeserializer::deserializeTv($document->documentElement);
24✔
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();
8✔
105
        if (false === @$document->load($filename)) {
8✔
106
            throw new RuntimeException(libxml_get_last_error()->message, 500);
×
107
        }
108

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

112
        return $statistics->getStatistics();
8✔
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 = [
6✔
126
            '<?xml version="1.0" encoding="utf-8"?>',
8✔
127
            '<!DOCTYPE tv SYSTEM "xmltv.dtd">',
8✔
128
            $tv->xmlSerialize(),
8✔
129
        ];
6✔
130

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

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

139
        return $document->save($filename);
8✔
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