Passed
Push — master ( 0837cd...cb2cf5 )
by Pauli
02:00
created

BookmarkBusinessLayer::findByTrack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
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
13
 */
14
15
namespace OCA\Music\BusinessLayer;
16
17
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayer;
18
use \OCA\Music\AppFramework\Core\Logger;
19
20
use \OCA\Music\Db\BaseMapper;
21
use \OCA\Music\Db\BookmarkMapper;
22
use \OCA\Music\Db\Bookmark;
23
24
use \OCA\Music\Utility\Util;
25
26
use \OCP\AppFramework\Db\DoesNotExistException;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Db\DoesNotExistException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
29
class BookmarkBusinessLayer extends BusinessLayer {
30
	private $logger;
31
32
	public function __construct(
33
			BookmarkMapper $bookmarkMapper,
34
			Logger $logger) {
35
		parent::__construct($bookmarkMapper);
36
		$this->logger = $logger;
37
	}
38
39
	/**
40
	 * @param string $userId
41
	 * @param int $trackId
42
	 * @param int $position
43
	 * @param string|null $comment
44
	 * @return Bookmark
45
	 */
46
	public function addOrUpdate($userId, $trackId, $position, $comment) {
47
		$updateTime = new \DateTime();
48
		$updateTime = $updateTime->format(BaseMapper::SQL_DATE_FORMAT);
49
50
		try {
51
			$bookmark = $this->findByTrack($trackId, $userId);
52
		} catch (DoesNotExistException $e) {
53
			$bookmark = new Bookmark();
54
			$bookmark->setCreated($updateTime);
55
		}
56
57
		$bookmark->setUserId($userId);
58
		$bookmark->setTrackId($trackId);
59
		$bookmark->setPosition($position);
60
		$bookmark->setComment(Util::truncate($comment, 256));
61
		$bookmark->setUpdated($updateTime);
62
63
		return $this->mapper->insertOrUpdate($bookmark);
64
	}
65
66
	/**
67
	 * @param int $trackId
68
	 * @param string $userId
69
	 * @throws DoesNotExistException if such bookmark does not exist
70
	 * @return Bookmark
71
	 */
72
	public function findByTrack($trackId, $userId) {
73
		return $this->mapper->findByTrack($trackId, $userId);
74
	}
75
}
76