Get::setRequestData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2014, Tobia De Koninck hey--at--ledfan.be
4
 * This file is licensed under the AGPL version 3 or later.
5
 * See the COPYING file.
6
 */
7
8
namespace OCA\Chat\OCH\Push;
9
10
use OCA\Chat\Controller\OCH\ApiController;
11
use \OCA\Chat\OCH\ChatAPI;
12
use \OCA\Chat\OCH\Db\PushMessageMapper;
13
use \OCA\Chat\Db\DoesNotExistException;
14
use \OCP\AppFramework\Http\JSONResponse;
15
use \OCA\Chat\OCH\Responses\Error;
16
17
class Get extends ChatAPI{
18
19
	private $cycles = 0;
20
21
	private $pushMessages = array();
22
23
	/**
24
	 * @var $pushMessageMapper \OCA\Chat\OCH\Db\PushMessageMapper
25
	 */
26
	private $pushMessageMapper;
27
28
	public function __construct(
29
		PushMessageMapper $pushMessageMapper
30
	) {
31
		$this->pushMessageMapper = $pushMessageMapper;
32
	}
33
34
	public function setRequestData(array $requestData){
35
		$this->requestData = $requestData;
36
	}
37
38
	public function execute(){
39
		session_write_close();
40
		do {
41
			if ($this->cycles > 0){
42
				sleep(1);
43
			}
44
			try {
45
				$this->pushMessages = $this->pushMessageMapper->findBysSessionId($this->requestData['session_id']);
46
				break;
47
			} catch(DoesNotExistException $e){
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
48
			}
49
			$this->cycles++;
50
		} while ($this->cycles < 50);
51
52
		if(count($this->pushMessages) > 0) {
53
			$commands = array();
54
			foreach ($this->pushMessages as $pushMessage) {
55
				$command = json_decode($pushMessage->getCommand());
56
				$commands[$pushMessage->getId()] = $command;
57
			}
58
			return new JSONResponse(array('push_msgs' => $commands));
59
		} else {
60
			return new Error('push', 'get', ApiController::TIME_EXCEEDED);
61
		}
62
	}
63
}
64