Completed
Pull Request — master (#822)
by
unknown
19:08
created

OtoLayerController::create()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 2
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
     * @NoAdminRequired
25
	 * @param string $appName
26
	 * @param IRequest $request an instance of the request
27
	 * @param OtoLayerMapper $mapper the dbo mapper
28
	 * @param IUserSession $userSession
29
	 */
30
     public function __construct($appName, IRequest $request, OtoLayerMapper $mapper, IUserSession $userSession){
31
         parent::__construct($appName, $request);
32
		 
33
		$this->mapper = $mapper; 
34
		$this->userSession = $userSession;
35
		//$userSession has the user id we are looking for
36
     }
37
	  /**
38
      *
39
      * @NoAdminRequired 
40
	  * @CSRFRequired
41
	  *
42
	  * @param string $sourceId
43
	  * @param string $destId
44
      */
45
	 public function create($sourceId, $destId){
46
		$layer = new OtoLayer();
47
		//error log above ensures otos layer was created successfully
48
		$layer->setSourceId($sourceId);
49
		$layer->setDestId($destId);
50
		//next line gets user ID
51
		$uid = $this->userSession->getUser()->getUID();
52
		//stores it in a local variable uid to use in setUserId() for basic user verification 
53
		//the logs above are used for testing to ensure correct id has been retrieved
54
		//below we set layer to uid we retrieved
55
		$layer->setUserId($uid);
56
		//generate a password thats longer than 6 chars
57
		$layer->setPassword('');
58
		while( strlen($layer->getPassword()) < 6 ){
59
			$password = bin2hex(openssl_random_pseudo_bytes(16));
60
			$layer->setPassword($password);
61
			//converts password to url format
62
			$layer->slugify('password');
63
		}
64
		
65
		try{
66
			return new DataResponse($this->mapper->createGetId($layer));
67
		}catch(Exception $e){
68
			return new DataResponse([], Http::STATUS_NOT_FOUND);
69
		}
70
	 }
71
	 /**
72
     * @NoAdminRequired
73
	 */
74
	 public function deleteOtoLayer($otoLayerId){
75
		try{
76
			$this->mapper->deleteLayer($otoLayerId);
77
			return new DataResponse(True);
78
		}catch(Exception $e){
79
			return new DataResponse([], Http::STATUS_NOT_FOUND);
80
		}
81
	 }
82
	 /**
83
     * @NoAdminRequired
84
	 */
85
	 public function isSchedulingLayer($sourceId){
86
		try{
87
			$isSchedulingLayer = $this->mapper->isSchedulingLayer($sourceId);
88
			return new DataResponse($isSchedulingLayer);
89
		}catch (Exception $e){
90
			return new DataResponse(False);
91
		}
92
	}
93
	/**
94
     * @NoAdminRequired
95
	 */
96 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...
97
		$userId = $this->userSession->getUser()->getUID();
98
		try{
99
			return new DataResponse($this->mapper->findUserLayers($userId));
100
		}catch(Exception $e){
101
			return new DataResponse([], Http::STATUS_NOT_FOUND);
102
		}
103
	}
104
 }