Completed
Push — master ( 964799...0d228a )
by claudio
09:13
created

CalendarsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Calendars;
4
5
use Illuminate\Support\Facades\Response;
6
use Illuminate\Http\Request; //TODO fix this
7
use it\thecsea\caldav_client_adapter\simple_caldav_client\SimpleCaldavAdapter;
8
use plunner\Calendar;
9
use plunner\Http\Controllers\Controller;
10
use plunner\Http\Requests\Employees\CalendarRequest;
11
12
13
class CalendarsController extends Controller
14
{
15
    /**
16
     * ExampleController constructor.
17
     */
18 14
    public function __construct()
19
    {
20 14
        config(['auth.model' => \plunner\Employee::class]);
21 14
        config(['jwt.user' => \plunner\Employee::class]);
22 14
        $this->middleware('jwt.authandrefresh:mode-en');
23 14
    }
24
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31 2
    public function index()
32
    {
33
        //
34
        /**
35
         * @var $employee Employee
36
         */
37 2
        $employee = \Auth::user();
38 2
        return $employee->calendars()->with('caldav')->get();
39
    }
40
41
    /**
42
     * Store a newly created resource in storage.
43
     *
44
     * @param  CalendarRequest  $request
45
     * @return \Illuminate\Http\Response
46
     */
47 2
    public function store(CalendarRequest $request)
48
    {
49
        //
50 2
        $employee = \Auth::user();
51 2
        $input = $request->all();
52 2
        $calendar = $employee->calendars()->create($input);
53 2
        return $calendar;
54
    }
55
56
    /**
57
     * Store a newly created resource in storage with caldav.
58
     *
59
     * @param  CalendarRequest  $request
60
     * @return \Illuminate\Http\Response
61
     */
62
    public function storeCaldav(CalendarRequest $request)
63
    {
64
        //
65
        $this->validateCaldav($request);
66
        $employee = \Auth::user();
67
        $input = $request->all();
68
        $calendar = $employee->calendars()->create($input);
69
        if(isset($input['password']))
70
            $input['password'] = \Crypt::encrypt($input['password']);
71
        $calendar->caldav()->create($input);
72
        //TODO test
73
        //TODO validator
74
        //TODO return caldav info
75
        //TODO supprot function to create simple calendar
76
        return $calendar;
77
    }
78
79
    /**
80
     * Display the specified resource.
81
     *
82
     * @param  int  $id
83
     * @return \Illuminate\Http\Response
84
     */
85 6
    public function show($id)
86
    {
87
        //
88 6
        $calendar = Calendar::with('caldav')->findOrFail($id);
89 6
        $this->authorize($calendar);
90 4
        return $calendar;
91
    }
92
93
    /**
94
     * Update the specified resource in storage.
95
     *
96
     * @param  CalendarRequest  $request
97
     * @param  int  $id
98
     * @return \Illuminate\Http\Response
99
     */
100 2
    public function update(CalendarRequest $request, $id)
101
    {
102
        //
103 2
        $calendar = Calendar::findOrFail($id);
104 2
        $this->authorize($calendar);
105 2
        $input = $request->all();
106 2
        $caldav = $calendar->caldav;
107 2
        if($caldav){
108
            $this->validateCaldav($request);
109
        }
110 2
        if(isset($input['password']))
111 2
            $input['password'] = \Crypt::encrypt($input['password']);
112 2
        $calendar->update($input);
113
        //TODO test
114
        //TODO validator
115
        //TODO check if caldav exists?
116
117
        if($caldav)
118 2
            $caldav->update($input);
119 2
        return $calendar;
120
    }
121
122
    /**
123
     * Remove the specified resource from storage.
124
     *
125
     * @param  int  $id
126
     * @return \Illuminate\Http\Response
127
     */
128 2
    public function destroy($id)
129
    {
130
        //
131 2
        $calendar = Calendar::findOrFail($id);
132 2
        $this->authorize($calendar);
133 2
        $calendar->delete();
134 2
        return $calendar;
135
    }
136
137
    /**
138
     * Return a list of calendars name of a specif caldav calendar
139
     * @param Request $request
140
     * @return \Illuminate\Http\Response
141
     */
142
    public function getCalendars(Request $request)
143
    {
144
        //TODO VALIDATE
145
        //TODO test this
146
        try {
147
            $caldavClient = new SimpleCaldavAdapter();
148
            $caldavClient->connect($request->input('url'), $request->input('username'), $request->input('password'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('url') targeting Illuminate\Http\Request::input() can also be of type array; however, it\thecsea\caldav_client...aldavAdapter::connect() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $request->input('username') targeting Illuminate\Http\Request::input() can also be of type array; however, it\thecsea\caldav_client...aldavAdapter::connect() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Request::input() can also be of type array; however, it\thecsea\caldav_client...aldavAdapter::connect() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
149
            $calendars = $caldavClient->findCalendars();
150
            return array_keys($calendars);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_keys($calendars); (integer[]) is incompatible with the return type documented by plunner\Http\Controllers...ontroller::getCalendars of type Illuminate\Http\Response.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
151
        }catch (\it\thecsea\caldav_client_adapter\CaldavException $e)
152
        {
153
            return Response::json(['error' => $e->getMessage()],422);
154
        }
155
    }
156
157
    /**
158
     * @param Request $request
159
     */
160
    private function validateCaldav(Request $request)
161
    {
162
        $this->validate($request, [
163
            'url' => 'required|max:255',
164
            'username' => 'required|max:255',
165
            'password' => ((\Route::current()->getName() == 'companies.calendars.caldav')?'required':''),
166
            'calendar_name' => 'required|max:255',
167
        ]);
168
    }
169
}
170