Op::canInsertOp()   D
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 25
Code Lines 22

Duplication

Lines 16
Ratio 64 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 16
loc 25
ccs 0
cts 24
cp 0
rs 4.909
c 0
b 0
f 0
cc 9
eloc 22
nc 9
nop 3
crap 90
1
<?php
2
/**
3
 * ownCloud - Documents App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Documents\Db;
13
14
class Op extends \OCA\Documents\Db {
15
16
	const DB_TABLE = '`*PREFIX*documents_op`';
17
	
18
	protected $tableName = '`*PREFIX*documents_op`';
19
	
20
	protected $insertStatement = 'INSERT INTO `*PREFIX*documents_op` (`es_id`, `optype`, `member`, `opspec`) VALUES (?, ?, ?, ?)';
21
22
	public static function addOpsArray($esId, $memberId, $ops){
23
		$opObj = new Op();
24
		foreach ($ops as $op) {
25
			if (!$opObj->canInsertOp($esId, $memberId, $op)){
26
				continue;
27
			}
28
			$opObj->setData(array(
29
				$esId,
30
				$op['optype'],
31
				$memberId,
32
				json_encode($op)
33
			));
34
			$opObj->insert();
35
		}
36
37
		return $opObj->getHeadSeq($esId);
38
	}
39
	
40
	/**
41
	 * @returns "" when there are no Ops, or the seq of the last Op
42
	 */
43
	public function getHeadSeq($esId){
44
		$query = \OC::$server->getDatabaseConnection()->prepare('
45
			SELECT `seq`
46
			FROM ' . $this->tableName . '
47
			WHERE `es_id`=?
48
			ORDER BY `seq` DESC
49
			', 1);
50
		$result = $query->execute([$esId]);
51
		return !$result ? "" : $query->fetchColumn();
52
	}
53
	
54
	public function getOpsAfterJson($esId, $seq){
55
		$ops = $this->getOpsAfter($esId, $seq);
56
		if (!is_array($ops)){
57
			$ops = array();
58
		}
59
		$ops =  array_map(
60
				function($x){
61
					$decoded = json_decode($x['opspec'], true);
62
					$decoded['memberid'] = strval($decoded['memberid']);
63
					return $decoded;
64
				}, 
65
				$ops
66
		);
67
		return $ops;
68
	}
69
	
70
	public function getOpsAfter($esId, $seq){
71
		if ($seq == ""){
72
			$seq = -1;
73
		}
74
		$query = \OC::$server->getDatabaseConnection()->prepare('
75
			SELECT `opspec`
76
			FROM ' . self::DB_TABLE . '
77
			WHERE `es_id`=?
78
				AND `seq`>?
79
			ORDER BY `seq` ASC
80
			');
81
		$query->execute(array($esId, $seq));
82
		return $query->fetchAll();
83
	}
84
	
85
	public function addMember($esId, $memberId, $fullName, $userId, $color, $imageUrl){
86
		$op = array(
87
			'optype' => 'AddMember',
88
			'memberid' => (string) $memberId,
89
			'timestamp' => $this->getMillisecondsAsString(),
90
			'setProperties' => array(
91
				'fullName' => $fullName,
92
				'color' => $color,
93
				'imageUrl' => $imageUrl,
94
				'uid' => $userId,
95
			)
96
		);
97
		$this->insertOp($esId, $memberId, $op);
98
	}
99
	
100 View Code Duplication
	public function removeCursor($esId, $memberId){
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...
101
		$op = array(
102
			'optype' => 'RemoveCursor',
103
			'memberid' => (string) $memberId,
104
			'reason' => 'server-idle',
105
			'timestamp' => $this->getMillisecondsAsString()
106
		);
107
		$this->insertOp($esId, $memberId, $op);
108
	}
109
	
110
	public function removeMember($esId, $memberId){
111
		$op = array(
112
			'optype' => 'RemoveMember',
113
			'memberid' => (string) $memberId,
114
			'timestamp' => $this->getMillisecondsAsString()
115
		);
116
		$this->insertOp($esId, $memberId, $op);
117
	}
118
	
119
	//TODO: Implement https://github.com/kogmbh/WebODF/blob/master/webodf/lib/ops/OpUpdateMember.js#L95
120 View Code Duplication
	public function changeNick($esId, $memberId, $fullName){
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...
121
		$op = array(
122
			'optype' => 'UpdateMember',
123
			'memberid' => (string) $memberId,
124
			'timestamp' => $this->getMillisecondsAsString(),
125
			'setProperties' => array(
126
				'fullName' => $fullName,
127
			)
128
		);
129
		$this->insertOp($esId, $memberId, $op);
130
	}
131
	
132
	protected function insertOp($esId, $memberId, $op){
133
		if ($this->canInsertOp($esId, $memberId, $op)){
134
			$op = new Op(array(
135
				$esId,
136
				$op['optype'],
137
				$memberId,
138
				json_encode($op)
139
			));
140
			$op->insert();
141
		}
142
	}
143
	
144
	protected function canInsertOp($esId, $memberId, $op){
145
		$cursorOps = array('AddCursor', 'RemoveCursor');
146
		$memberOps = array('AddMember', 'RemoveMember');
147
		$result = true;
148
		
149
		switch ($op['optype']){
150 View Code Duplication
			case 'AddCursor':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
151
				$ops = $this->getFilteredMemberOps($esId, $memberId, $cursorOps);
152
				$result = !count($ops) || $ops[0]['optype'] === 'RemoveCursor';
153
				break;
154 View Code Duplication
			case 'RemoveCursor':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
155
				$ops = $this->getFilteredMemberOps($esId, $memberId, $cursorOps);
156
				$result = count($ops) && $ops[0]['optype'] === 'AddCursor';
157
				break;
158 View Code Duplication
			case 'AddMember':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
159
				$ops = $this->getFilteredMemberOps($esId, $memberId, $memberOps);
160
				$result = !count($ops) || $ops[0]['optype'] === 'RemoveMember';
161
				break;
162 View Code Duplication
			case 'RemoveMember':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
163
				$ops = $this->getFilteredMemberOps($esId, $memberId, $memberOps);
164
				$result = count($ops) && $ops[0]['optype'] === 'AddMember';
165
				break;
166
		}
167
		return $result;
168
	}
169
170
	protected function getFilteredMemberOps($esId, $memberId, $targetOps){
171
		$stmt = $this->buildInQuery('optype', $targetOps);
172
		$result = $this->execute('
173
			SELECT `optype` FROM ' . $this->tableName . '
174
			WHERE es_id=? AND member=? AND ' . $stmt . 'ORDER BY `seq` DESC',
175
			array_merge(array($esId, $memberId), $targetOps)
176
		);
177
		$ops = $result->fetchAll();
178
		if (!is_array($ops)){
179
			$ops = array();
180
		}
181
		return $ops;
182
	}
183
	
184
	protected function getMillisecondsAsString(){
185
		$microtime = microtime();
186
		list($usec, $sec) = explode(" ", $microtime);
187
		$milliseconds = $sec.substr($usec, 2, 3);
188
		return $milliseconds;
189
	}
190
}
191