1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace plunner\Http\Controllers\Employees\Calendars; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use it\thecsea\caldav_client_adapter\simple_caldav_client\SimpleCaldavAdapter; |
7
|
|
|
use plunner\Calendar; |
8
|
|
|
use plunner\Http\Controllers\Controller; |
9
|
|
|
use plunner\Http\Requests\Employees\CalendarRequest; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class CalendarsController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* ExampleController constructor. |
16
|
|
|
*/ |
17
|
14 |
|
public function __construct() |
18
|
|
|
{ |
19
|
14 |
|
config(['auth.model' => \plunner\Employee::class]); |
20
|
14 |
|
config(['jwt.user' => \plunner\Employee::class]); |
21
|
14 |
|
$this->middleware('jwt.authandrefresh:mode-en'); |
22
|
14 |
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Display a listing of the resource. |
27
|
|
|
* |
28
|
|
|
* @return \Illuminate\Http\Response |
29
|
|
|
*/ |
30
|
2 |
|
public function index() |
31
|
|
|
{ |
32
|
|
|
// |
33
|
|
|
/** |
34
|
|
|
* @var $employee Employee |
35
|
|
|
*/ |
36
|
2 |
|
$employee = \Auth::user(); |
37
|
2 |
|
return $employee->calendars; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Store a newly created resource in storage. |
42
|
|
|
* |
43
|
|
|
* @param CalendarRequest $request |
44
|
|
|
* @return \Illuminate\Http\Response |
45
|
|
|
*/ |
46
|
2 |
|
public function store(CalendarRequest $request) |
47
|
|
|
{ |
48
|
|
|
// |
49
|
2 |
|
$employee = \Auth::user(); |
50
|
2 |
|
$input = $request->all(); |
51
|
2 |
|
$calendar = $employee->calendars()->create($input); |
52
|
2 |
|
return $calendar; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Display the specified resource. |
57
|
|
|
* |
58
|
|
|
* @param int $id |
59
|
|
|
* @return \Illuminate\Http\Response |
60
|
|
|
*/ |
61
|
6 |
|
public function show($id) |
62
|
|
|
{ |
63
|
|
|
// |
64
|
6 |
|
$calendar = Calendar::findOrFail($id); |
65
|
6 |
|
$this->authorize($calendar); |
66
|
4 |
|
return $calendar; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Update the specified resource in storage. |
71
|
|
|
* |
72
|
|
|
* @param CalendarRequest $request |
73
|
|
|
* @param int $id |
74
|
|
|
* @return \Illuminate\Http\Response |
75
|
|
|
*/ |
76
|
2 |
|
public function update(CalendarRequest $request, $id) |
77
|
|
|
{ |
78
|
|
|
// |
79
|
2 |
|
$calendar = Calendar::findOrFail($id); |
80
|
2 |
|
$this->authorize($calendar); |
81
|
2 |
|
$input = $request->all(); |
82
|
2 |
|
$calendar->update($input); |
83
|
2 |
|
return $calendar; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Remove the specified resource from storage. |
88
|
|
|
* |
89
|
|
|
* @param int $id |
90
|
|
|
* @return \Illuminate\Http\Response |
91
|
|
|
*/ |
92
|
2 |
|
public function destroy($id) |
93
|
|
|
{ |
94
|
|
|
// |
95
|
2 |
|
$calendar = Calendar::findOrFail($id); |
96
|
2 |
|
$this->authorize($calendar); |
97
|
2 |
|
$calendar->delete(); |
98
|
2 |
|
return $calendar; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return a list of calendars name of a specif caldav calendar |
103
|
|
|
* @param Request $request |
104
|
|
|
* @return \Illuminate\Http\Response |
105
|
|
|
*/ |
106
|
|
|
public function getCalendars(Request $request) |
107
|
|
|
{ |
108
|
|
|
//TODO VALIDATE |
109
|
|
|
//TODO test this |
110
|
|
|
try { |
111
|
|
|
$caldavClient = new SimpleCaldavAdapter(); |
112
|
|
|
$caldavClient->connect($request->get('url'), $request->get('username'), $request->get('password')); |
113
|
|
|
$calendars = $caldavClient->findCalendars(); |
114
|
|
|
return array_keys($calendars); |
|
|
|
|
115
|
|
|
}catch (\it\thecsea\caldav_client_adapter\CaldavException $e) |
116
|
|
|
{ |
117
|
|
|
return Response::json(['error' => $e->getMessage()],422); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.