Passed
Push — master ( 7b42d0...d80bc5 )
by Pauli
04:15 queued 21s
created

BookmarkBusinessLayer::updateByEntry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 5
dl 0
loc 7
rs 10
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 Gavin E <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Gavin E 2020
12
 * @copyright Pauli Järvinen 2020 - 2025
13
 */
14
15
namespace OCA\Music\BusinessLayer;
16
17
use OCA\Music\AppFramework\BusinessLayer\BusinessLayer;
18
use OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
19
use OCA\Music\AppFramework\Core\Logger;
20
21
use OCA\Music\Db\BookmarkMapper;
22
use OCA\Music\Db\Bookmark;
23
use OCA\Music\Db\MatchMode;
24
use OCA\Music\Db\SortBy;
25
use OCA\Music\Utility\StringUtil;
26
27
use OCP\AppFramework\Db\DoesNotExistException;
28
29
/**
30
 * Base class functions with the actually used inherited types to help IDE and Scrutinizer:
31
 * @method Bookmark find(int $bookmarkId, string $userId)
32
 * @method Bookmark[] findAll(string $userId, int $sortBy=SortBy::Name, ?int $limit=null, ?int $offset=null)
33
 * @method Bookmark[] findAllByName(string $name, string $userId, int $matchMode=MatchMode::Exact, ?int $limit=null, ?int $offset=null)
34
 * @property BookmarkMapper $mapper
35
 * @phpstan-extends BusinessLayer<Bookmark>
36
 */
37
class BookmarkBusinessLayer extends BusinessLayer {
38
	private Logger $logger;
39
40
	public function __construct(BookmarkMapper $bookmarkMapper, Logger $logger) {
41
		parent::__construct($bookmarkMapper);
42
		$this->logger = $logger;
43
	}
44
45
	/**
46
	 * @param int $type One of [Bookmark::TYPE_TRACK, Bookmark::TYPE_PODCAST_EPISODE]
47
	 */
48
	public function addOrUpdate(string $userId, int $type, int $entryId, int $position, ?string $comment) : Bookmark {
49
		$bookmark = new Bookmark();
50
		$bookmark->setUserId($userId);
51
		$bookmark->setType($type);
52
		$bookmark->setEntryId($entryId);
53
		$bookmark->setPosition($position);
54
		$bookmark->setComment(StringUtil::truncate($comment, 256));
55
56
		return $this->mapper->insertOrUpdate($bookmark);
57
	}
58
59
	/**
60
	 * @param ?string $comment Property is not updated if null passed
61
	 * @throws BusinessLayerException if such bookmark does not exist
62
	 */
63
	public function updateByEntry(string $userId, int $type, int $entryId, int $position, ?string $comment) : Bookmark {
64
		$bookmark = $this->findByEntry($type, $entryId, $userId);
65
		$bookmark->setPosition($position);
66
		if ($comment !== null) {
67
			$bookmark->setComment(StringUtil::truncate($comment, 256));
68
		}
69
		return $this->mapper->update($bookmark);
70
	}
71
72
	/**
73
	 * @param int $type One of [Bookmark::TYPE_TRACK, Bookmark::TYPE_PODCAST_EPISODE]
74
	 * @throws BusinessLayerException if such bookmark does not exist
75
	 */
76
	public function findByEntry(int $type, int $entryId, string $userId) : Bookmark {
77
		try {
78
			return $this->mapper->findByEntry($type, $entryId, $userId);
79
		} catch (DoesNotExistException $ex) {
80
			throw new BusinessLayerException($ex->getMessage());
81
		}
82
	}
83
}
84