Completed
Pull Request — master (#822)
by
unknown
20:37
created

OtoLayerController::isSchedulingLayer()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 3
nop 1
1
<?php
2
 namespace OCA\Calendar\Controller;
3
4
 use Exception;
5
6
 use OCP\IRequest;
7
 use OCP\IUserSession;
8
 use OCP\AppFramework\Http;
9
 use OCP\AppFramework\Http\DataResponse;
10
 use OCP\AppFramework\Controller;
11
 use OCP\AppFramework\Db\DoesNotExistException;
12
 use OCP\AppFramework\Db\MultipleObjectsReturnedException;
13
14
 use OCA\Calendar\Db\OtoLayer;
15
 use OCA\Calendar\Db\OtoLayerMapper;
16
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
17
18
 
19
 class OtoLayerController extends Controller {
20
	//the param added for user session allows us to get the uid
21
	private $userSession;
22
	private $mapper;
23
	/**
24
	 * @param string $appName
25
	 * @param IRequest $request an instance of the request
26
	 * @param OtoLayerMapper $mapper the dbo mapper
27
	 * @param IUserSession $userSession
28
	 */
29
     public function __construct($appName, IRequest $request, OtoLayerMapper $mapper, IUserSession $userSession){
30
         parent::__construct($appName, $request);
31
		 
32
		$this->mapper = $mapper; 
33
		$this->userSession = $userSession;
34
		//$userSession has the user id we are looking for
35
     }
36
	  /**
37
      * 
38
	  * @CSRFRequired
39
	  *
40
	  * @param string $sourceId
41
	  * @param string $destId
42
      */
43
	 public function create($sourceId, $destId){
44
		$layer = new OtoLayer();
45
		//error log above ensures otos layer was created successfully
46
		$layer->setSourceId($sourceId);
47
		$layer->setDestId($destId);
48
		//next line gets user ID
49
		$uid = $this->userSession->getUser()->getUID();
50
		//stores it in a local variable uid to use in setUserId() for basic user verification 
51
		//the logs above are used for testing to ensure correct id has been retrieved
52
		//below we set layer to uid we retrieved
53
		$layer->setUserId($uid);
54
		//generate a password thats longer than 6 chars
55
		$layer->setPassword('');
56
		while( strlen($layer->getPassword()) < 6 ){
57
			$password = bin2hex(openssl_random_pseudo_bytes(16));
58
			$layer->setPassword($password);
59
			//converts password to url format
60
			$layer->slugify('password');
61
		}
62
		
63
		try{
64
			return new DataResponse($this->mapper->createGetId($layer));
65
		}catch(Exception $e){
66
			return new DataResponse([], Http::STATUS_NOT_FOUND);
67
		}
68
	 }
69
	 
70
	 public function deleteOtoLayer($otoLayerId){
71
		try{
72
			$this->mapper->deleteLayer($otoLayerId);
73
			return new DataResponse(True);
74
		}catch(Exception $e){
75
			return new DataResponse([], Http::STATUS_NOT_FOUND);
76
		}
77
	 }
78
	 
79
	 public function isSchedulingLayer($sourceId){
80
		try{
81
			$isSchedulingLayer = $this->mapper->isSchedulingLayer($sourceId);
82
			return new DataResponse($isSchedulingLayer);
83
		}catch (Exception $e){
84
			return new DataResponse(False);
85
		}
86
	}
87
	
88 View Code Duplication
	public function findUserLayers(){
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...
89
		$userId = $this->userSession->getUser()->getUID();
90
		try{
91
			return new DataResponse($this->mapper->findUserLayers($userId));
92
		}catch(Exception $e){
93
			return new DataResponse([], Http::STATUS_NOT_FOUND);
94
		}
95
	}
96
 }