ApiController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 4
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\Controller\OCH;
9
10
use \OCA\Chat\OCH\Responses\Success;
11
use \OCA\Chat\OCH\Responses\Error;
12
use \OCA\Chat\OCH\Exceptions\RequestDataInvalid;
13
use \OCP\AppFramework\Http\JSONResponse;
14
use \OCP\AppFramework\Controller;
15
use \OCP\IRequest;
16
use \OCA\Chat\Db\DBException;
17
use \OCA\Chat\App\Chat;
18
use \OCA\Chat\App\Container;
19
20
class ApiController extends Controller {
21
22
	/**
23
	 * Error codes used by the API
24
	 */
25
	const INVALID_HTTP_TYPE = 0;
26
	const COMMAND_NOT_FOUND = 1;
27
	const PUSH_ACTION_NOT_FOUND = 2;
28
	const DATA_ACTION_NOT_FOUND = 3;
29
	const NO_SESSION_ID = 6;
30
	const USER_NOT_EQUAL_TO_OC_USER = 7;
31
	const NO_TIMESTAMP = 8;
32
	const NO_CONV_ID = 9;
33
	const NO_USER_TO_INVITE = 10;
34
	const USER_EQUAL_TO_USER_TO_INVITE = 11;
35
	const USER_TO_INVITE_NOT_OC_USER = 12;
36
	const NO_CHAT_MSG = 13;
37
	const NO_USER = 14;
38
    const NOT_OWNER_OF_FILE =15;
39
	const TIME_EXCEEDED = 16;
40
41
	public function __construct($appName, IRequest $request,  Chat $app, Container $container){
42
		parent::__construct($appName, $request);
43
		$this->app = $app;
44
		$this->container = $container;
45
	}
46
47
	/**
48
	 * Routes the API Request to the correct Class
49
	 * There are 3 types: command, data and push
50
	 * @param string $type
51
	 * @param array $data
52
	 * @return JSONResponse
53
	 * @NoAdminRequired
54
	 */
55
	public function route($type, $data){
56
		session_write_close();
57
		list($requestType, $action, $httpType) = explode("::", $type);
58
59
		if($httpType === "request"){
60
			if(!empty($data['session_id'])){
61
				if(!empty($data['user'])){
62
					if($data['user']['id'] === $this->app->getUserId()){
63
						try{
64
							switch($requestType){
65
								case "command":
66
									$possibleCommands = array('greet', 'join', 'invite', 'send_chat_msg', 'online', 'offline', 'start_conv', 'delete_init_conv', 'attach_file', 'remove_file');
67
									if(in_array($action, $possibleCommands)){
68
										$commandClass = $this->container->query($this->convertClassName($action) . 'Command');
69
										$commandClass->setRequestData($data);
70
										$data = $commandClass->execute();
71
										if($data){
72
											return new Success("command", $action, $data);
73
										} else {
74
											return new Success("command", $action);
75
										}
76
									} else {
77
										return new Error("command", $action, self::COMMAND_NOT_FOUND);
78
									}
79
									break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
80
								case "push":
81
									$possibleCommands = array('get', 'delete');
82
									if(in_array($action, $possibleCommands)){
83
										$pushClass = $this->container->query($this->convertClassName($action) . 'Push');
84
										$pushClass->setRequestData($data);
85
										return $pushClass->execute();
86
									} else {
87
										return new Error("push", $action, self::PUSH_ACTION_NOT_FOUND);
88
									}
89
									break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
90
								case "data":
91
									$possibleCommands = array('messages', 'get_users');
92
									if(in_array($action, $possibleCommands)){
93
										$dataClass = $this->container->query($this->convertClassName($action) . 'Data');
94
										$dataClass->setRequestData($data);
95
										$data = $dataClass->execute();
96
										if($data){
97
											return new Success("data", $action, $data);
98
										} else {
99
											return new Success("data", $action);
100
										}
101
									} else {
102
										return new Error("data", $action, self::DATA_ACTION_NOT_FOUND);
103
									}
104
									break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
105
							}
106
						}catch(DBException $e){
107
							return new Error($requestType, $action, "ERROR::DB::" . $e->getMessage());
108
						}
109
						catch(RequestDataInvalid $e){
110
							return new Error($requestType, $action, $e->getMessage());
111
						}
112
					} else {
113
						return new Error($requestType, $action, self::USER_NOT_EQUAL_TO_OC_USER);
114
					}
115
				} else {
116
					return new Error($requestType, $action,  self::NO_USER);
117
				}
118
			} else {
119
				return new Error($requestType, $action,  self::NO_SESSION_ID);
120
			}
121
		} else {
122
			return new Error($requestType, $action, self::INVALID_HTTP_TYPE);
123
		}
124
	}
125
126
	/**
127
	 * Helper function to transform the $action to a correct classname
128
	 * @param $class
129
	 * @return string
130
	 */
131
	private function convertClassName($class){
132
		$newClass = '';
133
		$parts = explode("_", $class);
134
		foreach($parts as $part){
135
			$newClass .= ucfirst($part);
136
		}
137
		return $newClass;
138
	}
139
}
140