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

OtoConfirmationController::create()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 4
1
<?php
2
 namespace OCA\Calendar\Controller;
3
4
 use Exception;
5
6
 use OCP\IRequest;
7
 use OCP\AppFramework\Http;
8
 use OCP\AppFramework\Http\DataResponse;
9
 use OCP\AppFramework\Controller;
10
 use OCA\Calendar\Db\OtoConfirmation;
11
 use OCA\Calendar\Db\OtoConfirmationMapper;
12
 use OCA\Calendar\Service\OtoLayerService;
13
 use OCA\Calendar\Service\OtoConfirmationService;
14
 use OCP\IUserSession;
15
 //using the IUserSession above lets us get info about the user
16
 
17
 class OtoConfirmationController extends Controller {
18
	
19
	private $mapper;
20
	private $foo;
21
22
23
	/**
24
	 * @param string $appName
25
	 * @param IRequest $request an instance of the request
26
	 * @param OtoConfirmationMapper $mapper 
27
	 * @param OtoConfirmationService $otoConfirmationService
28
	 * @param OtoLayerService $otoLayerService
29
	 * @param IUserSession $userSession
30
	 */
31
	 public function __construct($appName, IRequest $request, OtoConfirmationMapper $mapper,OtoConfirmationService $otoConfirmationService, OtoLayerService $otoLayerService, IUserSession $userSession){
32
         parent::__construct($appName, $request);
33
		 $this->mapper = $mapper;
34
		 $this->otoLayerService = $otoLayerService;
35
		 $this->otoConfirmationService = $otoConfirmationService;
36
		 $this->userSession = $userSession;
37
		// $this->userId = $userSession->getUser()->getUID();
38
     } 
39
	 /**
40
      * @PublicPage
41
	  * @NoCSRFRequired
42
	  * @param string $otoLayerId
43
	  * @param string $password
44
	  * @param string $eventId
45
	  * @param string $name
46
	  * @param string $password
47
      */
48
	 public function create($otoLayerId, $password, $eventId, $name){
49
		 //check password first, refuse connection if not matching
50
		if(! $this->otoLayerService->passwordCheck($otoLayerId,$password) ){
51
			return new DataResponse([], Http::STATUS_NOT_FOUND);
52
		}
53
		//check that we can still confirm this event, if not refuse the connection
54
		if(! $this->mapper->canConfirm($otoLayerId) ){
55
			return new DataResponse([], Http::STATUS_NOT_FOUND);
56
		}
57
		$confirmation = new OtoConfirmation();
58
		//below log message is only present if we passed these tests
59
		$confirmation->setOtoLayerId($otoLayerId);
60
		$confirmation->setName($name);
61
		$confirmation->setEventId($eventId);
62
		//populate confirmation with relevant information
63
		try{
64
			$foo = new DataResponse($this->mapper->insert($confirmation));
65
			return $foo;
66
			//attempt to insert the confirmation, no log message if successful
67
		}catch(Exception $e){
68
			//catch exceptions and add them to our phplog for debugging or review
69
			return new DataResponse([], Http::STATUS_NOT_FOUND);
70
			//refuse the connection
71
		}
72
	 }
73
	 //returns a data response that contains all of the confirmations for a user 
74
	 //along with their otoLayer's sourceId and destinationId
75 View Code Duplication
	 public function getConfirmationsByUser(){
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...
76
		//user ID is passed in
77
		$userId = $this->userSession->getUser()->getUID();
78
		try{
79
			return new DataResponse($this->mapper->getConfirmationsByUser($userId));
80
		}catch(Exception $e){
81
			return new DataResponse([],Http::STATUS_NOT_FOUND);
82
		}
83
	 }
84
	 //deletes a layer and all of it's confirmations
85
	 public function deleteConfirmationsLayers($otoLayerId){
86
		try{
87
			$this->otoConfirmationService->deleteConfirmationsByOtoLayer($otoLayerId);
88
			$this->otoLayerService->deleteLayer($otoLayerId);
89
			return new DataResponse(True);
90
		}catch(Exception $e){
91
			//catch any problems that might arise when deleting a layer
92
			return new DataResponse(False);
93
		}
94
	 }
95
	 //deletes a layer and all of it's confirmations
96
	 public function deleteBySourceId($sourceId){
97
		try{
98
			$userId = $this->userSession->getUser()->getUID();
99
			$this->otoConfirmationService->deleteBySourceId($sourceId,$userId);
100
			$this->otoLayerService->deleteBySourceId($sourceId,$userId);
101
		return new DataResponse(True);
102
		}catch(Exception $e){
103
			//catch any problems that might arise when deleting a layer
104
			return new DataResponse(False);
105
		}
106
	 }
107
 }