Passed
Pull Request — master (#752)
by
unknown
02:07
created

BookmarkMapper::removePlayQueueBookmarks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 1
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
 * @copyright Gavin E 2020
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\IDBConnection;
0 ignored issues
show
Bug introduced by
The type OCP\IDBConnection 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...
16
17
class BookmarkMapper extends BaseMapper {
18
	public function __construct(IDBConnection $db) {
19
		parent::__construct($db, 'music_bookmarks', '\OCA\Music\Db\Bookmark', 'comment');
20
	}
21
22
	/**
23
	 * @param string $condition
24
	 */
25
	private function makeSelectQuery($condition=null) {
26
		return 'SELECT * ' .
27
			'FROM `*PREFIX*music_bookmarks` ' .
28
			'WHERE `user_id` = ? ' . $condition;
29
	}
30
31
	/**
32
	 * @param string $userId
33
	 * @param integer $limit
34
	 * @param integer $offset
35
	 * @return Bookmark[]
36
	 */
37
	public function findAll($userId, $limit=null, $offset=null) {
38
		return $this->findEntities($this->makeSelectQuery(), [ $userId ], $limit, $offset);
39
	}
40
41
	/**
42
	 * @param string $userId
43
	 * @param string $trackId
44
	 * @param string $position
45
	 * @param string $comment
46
	 * @return Bookmark[]
47
	 */
48
	public function create($userId, $trackId, $position, $comment) {
49
    // ensure no existing record exists
50
    $this->remove($userId, $trackId);
51
52
    $bookmark = new Bookmark();
53
    $bookmark->setUserId($userId);
54
    $bookmark->setTrackId($trackId);
55
    $bookmark->setPosition($position);
56
    $bookmark->setComment($comment);
57
58
    return $this->insertOrUpdate($bookmark);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->insertOrUpdate($bookmark) returns the type OCA\Music\Db\Entity which is incompatible with the documented return type OCA\Music\Db\Bookmark[].
Loading history...
59
	}
60
61
	/**
62
	 * @param string $userId
63
	 * @param string $trackId
64
	 * @return Bookmark[]
65
	 */
66
	public function remove($userId, $trackId) {
67
		$sql = 'DELETE FROM `' . $this->getTableName() . '` WHERE `user_id` = ? AND `track_id` = ?';
68
		$this->execute($sql, [ $userId, $trackId ]);
69
	}
70
71
  public function findPlayQueueBookmark($userId) {
72
    $retVal = \array_shift($this->findEntities(
0 ignored issues
show
Bug introduced by
$this->findEntities('SEL...` < 0', array($userId)) cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
    $retVal = \array_shift(/** @scrutinizer ignore-type */ $this->findEntities(
Loading history...
73
        'SELECT * FROM `' . $this->getTableName() . '` WHERE `user_id` = ? AND `track_id` < 0',
74
        [ $userId ]
75
    ));
76
77
    // remove negative value
78
    if ($retVal !== null) {
79
      $retVal->trackId = -$retVal->trackId;
80
    }
81
82
    return $retVal;
83
  }
84
85
  public function removePlayQueueBookmarks($userId) {
86
    return $this->execute(
87
        'DELETE FROM `' . $this->getTableName() . '` WHERE `user_id` = ? AND `track_id` < 0',
88
        [ $userId ]);
89
  }
90
91
  protected function findUniqueEntity($entity) {
92
    return $this->findEntity(
93
        'SELECT * FROM `' . $this->getTableName() . '` WHERE `user_id` = ? AND `track_id` < 0',
94
        [ $entity->getUserId(), $entity->getTrackId() ]
95
    );
96
  }
97
}
98