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

webeweb / xmltv-library / 8306651198

16 Mar 2024 08:55AM UTC coverage: 99.723%. Remained the same
8306651198

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\Provider\Helper\GuzzleHelper;
22
use WBW\Library\XmlTv\Model\Tv;
23
use WBW\Library\XmlTv\Serializer\SerializerHelper;
24
use WBW\Library\XmlTv\Serializer\XmlDeserializer;
25
use WBW\Library\XmlTv\Statistic\Statistic;
26
use WBW\Library\XmlTv\Statistic\Statistics;
27

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

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

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

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

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

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

70
        return static::readXml($filename, $logger);
8✔
71
    }
72

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

83
        SerializerHelper::setLogger($logger);
24✔
84

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

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

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

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

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

113
        return $statistics->getStatistics();
8✔
114
    }
115

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

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

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

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

140
        return $document->save($filename);
8✔
141
    }
142
}
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

© 2026 Coveralls, Inc