ChangeHelper::boardChanged()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Db;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
use OCP\ICache;
28
use OCP\ICacheFactory;
29
use OCP\IDBConnection;
30
use OCP\IRequest;
31
use OCP\IUserManager;
32
use OCP\IGroupManager;
33
34
class ChangeHelper {
35
36
	const TYPE_BOARD = 'boardChanged';
37
	const TYPE_CARD = 'cardChanged';
38
39
	private $db;
40
41
	public function __construct(
42
		IDBConnection $db,
43
		ICacheFactory $cacheFactory,
44
		IRequest $request,
45
		$userId
46
	) {
47
		$this->db = $db;
48
		$this->cache = $cacheFactory->createDistributed('deck_changes');
0 ignored issues
show
Bug introduced by
The property cache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
		$this->request = $request;
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
		$this->userId = $userId;
0 ignored issues
show
Bug introduced by
The property userId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
	}
52
53 View Code Duplication
	public function boardChanged($boardId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
		$time = time();
55
		$etag = md5($time . microtime());
56
		$this->cache->set(self::TYPE_BOARD . '-' . $boardId, $etag);
57
		$sql  = 'UPDATE `*PREFIX*deck_boards` SET `last_modified` = ? WHERE `id` = ?';
58
		$this->db->executeUpdate($sql, [$time, $boardId]);
59
	}
60
61
	public function cardChanged($cardId, $updateCard = true) {
62
		$time = time();
63
		$etag = md5($time . microtime());
64
		$this->cache->set(self::TYPE_CARD . '-' .$cardId, $etag);
65
		if ($updateCard) {
66
			$sql = 'UPDATE `*PREFIX*deck_cards` SET `last_modified` = ?, `last_editor` = ? WHERE `id` = ?';
67
			$this->db->executeUpdate($sql, [time(), $this->userId, $cardId]);
68
		}
69
70
		$sql = 'SELECT s.board_id as id, c.stack_id as stack_id FROM `*PREFIX*deck_stacks` as s inner join `*PREFIX*deck_cards` as c ON c.stack_id = s.id WHERE c.id = ?';
71
		$result = $this->db->executeQuery($sql, [$cardId]);
72
		if ($row = $result->fetch()) {
73
			$this->boardChanged($row['id']);
74
			$this->stackChanged($row['stack_id']);
75
		}
76
	}
77
78 View Code Duplication
	public function stackChanged($stackId, $updateBoard = true) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
		$time = time();
80
		$etag = md5($time . microtime());
81
		$this->cache->set(self::TYPE_CARD . '-' .$stackId, $etag);
82
		if ($updateBoard) {
83
			$sql = 'UPDATE `*PREFIX*deck_stacks` SET `last_modified` = ? WHERE `id` = ?';
84
			$this->db->executeUpdate($sql, [time(), $stackId]);
85
		}
86
	}
87
88
	public function checkEtag($type, $id) {
89
		$etag = $this->getEtag($type, $id);
90
		if ($this->request->getHeader('If-None-Match') === $etag) {
91
			return true;
92
		}
93
		return false;
94
	}
95
96
	public function getEtag($type, $id) {
97
		$entry = $this->cache->get($type . '-' .$id);
98
		if ($entry === 'null') {
99
			return '';
100
		}
101
		return $entry;
102
	}
103
104
}
105