Passed
Push — feature/786_podcasts ( f026ee )
by Pauli
11:46
created

parseChannelDataFromXml()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 16
rs 9.8333
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2021
11
 */
12
13
namespace OCA\Music\BusinessLayer;
14
15
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayer;
16
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
17
use \OCA\Music\AppFramework\Core\Logger;
18
19
use \OCA\Music\Db\BaseMapper;
20
use \OCA\Music\Db\PodcastChannelMapper;
21
use \OCA\Music\Db\PodcastChannel;
22
23
use \OCA\Music\Utility\Util;
24
25
26
/**
27
 * Base class functions with the actually used inherited types to help IDE and Scrutinizer:
28
 * @method PodcastChannel find(int $channelId, string $userId)
29
 * @method PodcastChannel[] findAll(
30
 *			string $userId, int $sortBy=SortBy::None, int $limit=null, int $offset=null,
31
 *			?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null)
32
 * @method PodcastChannel[] findAllByName(
33
 *			string $name, string $userId, bool $fuzzy=false, int $limit=null, int $offset=null,
34
 *			?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null)
35
 */
36
class PodcastChannelBusinessLayer extends BusinessLayer {
37
	protected $mapper; // eclipse the definition from the base class, to help IDE and Scrutinizer to know the actual type
38
	private $logger;
39
40
	public function __construct(PodcastChannelMapper $mapper, Logger $logger) {
41
		parent::__construct($mapper);
42
		$this->mapper = $mapper;
43
		$this->logger = $logger;
44
	}
45
46
	public function create(string $userId, string $rssUrl, string $rssContent, \SimpleXMLElement $xmlNode) : PodcastChannel {
47
		$channel = new PodcastChannel();
48
		self::parseChannelDataFromXml($xmlNode, $channel);
49
50
		$channel->setUserId( $userId );
51
		$channel->setRssUrl( Util::truncate($rssUrl, 2048) );
52
		$channel->setRssHash( \hash('md5', $rssUrl) );
53
		$channel->setContentHash( \hash('md5', $rssContent) );
54
		$channel->setUpdateChecked( \date(BaseMapper::SQL_DATE_FORMAT) );
55
56
		return $this->mapper->insert($channel);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->mapper->insert($channel) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\Music\Db\PodcastChannel.
Loading history...
57
	}
58
59
	/**
60
	 * @param PodcastChannel $channel Input/output parameter for the channel
61
	 * @param string $rssContent Raw content of the RSS feed
62
	 * @param \SimpleXMLElement $xmlNode <channel> node parsed from the RSS feed
63
	 * @return boolean true if the new content differed from the previously cached content
64
	 */
65
	public function updateChannel(PodcastChannel &$channel, string $rssContent, \SimpleXMLElement $xmlNode) {
66
		$contentChanged = false;
67
		$contentHash = \hash('md5', $rssContent);
68
69
		if ($channel->getContentHash() !== $contentHash) {
70
			$contentChanged = true;
71
			self::parseChannelDataFromXml($xmlNode, $channel);
72
			$channel->setContentHash($contentHash);
73
		}
74
		$channel->setUpdateChecked( \date(BaseMapper::SQL_DATE_FORMAT) );
75
76
		$this->update($channel);
77
		return $contentChanged;
78
	}
79
80
	private static function parseChannelDataFromXml(\SimpleXMLElement $xmlNode, PodcastChannel &$channel) : void {
81
		$itunesNodes = $xmlNode->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
82
83
		// TODO: handling for invalid data
84
		$channel->setSourceUpdated( \date(BaseMapper::SQL_DATE_FORMAT,
85
				\strtotime((string)($xmlNode->lastBuildDate ?: $xmlNode->pubDate))) );
86
		$channel->setTitle( Util::truncate((string)$xmlNode->title, 256) );
87
		$channel->setLinkUrl( Util::truncate((string)$xmlNode->link, 2048) );
88
		$channel->setLanguage( Util::truncate((string)$xmlNode->language, 32) );
89
		$channel->setCopyright( Util::truncate((string)$xmlNode->copyright, 256) );
90
		$channel->setAuthor( Util::truncate((string)($xmlNode->author ?: $itunesNodes->author), 256) );
91
		$channel->setDescription( (string)($xmlNode->description ?: $itunesNodes->summary) );
92
		$channel->setImageUrl( (string)$xmlNode->image->url );
93
		$channel->setCategory( \implode(', ', \array_map(function ($category) {
94
			return $category->attributes()['text'];
95
		}, \iterator_to_array($itunesNodes->category, false))) );
96
	}
97
98
}
99