Completed
Push — master ( cb49ac...f14b58 )
by claudio
13:10
created

CalendarsController::update()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.0534
cc 4
eloc 13
nc 8
nop 2
crap 4
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\Calendar\CalendarRequest;
11
12
13
class CalendarsController extends Controller
14
{
15
    /**
16
     * ExampleController constructor.
17
     */
18 20
    public function __construct()
19
    {
20 20
        config(['auth.model' => \plunner\Employee::class]);
21 20
        config(['jwt.user' => \plunner\Employee::class]);
22 20
        $this->middleware('jwt.authandrefresh:mode-en');
23 20
    }
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
     * <strong>CAUTION:</strong> this method returns only calendar data, not caldav
59
     *
60
     * @param  CalendarRequest  $request
61
     * @return \Illuminate\Http\Response
62
     */
63 2
    public function storeCaldav(CalendarRequest $request)
64
    {
65
        //
66 2
        $this->validateCaldav($request);
67 2
        $employee = \Auth::user();
68 2
        $input = $request->all();
69 2
        $calendar = $employee->calendars()->create($input);
70 2
        if(isset($input['password']))
71 2
            $input['password'] = \Crypt::encrypt($input['password']);
72 2
        $calendar->caldav()->create($input);
73
        //TODO test
74
        //TODO validator
75
        //TODO return caldav info
76
        //TODO supprot function to create simple calendar
77 2
        return $calendar;
78
    }
79
80
    /**
81
     * Display the specified resource.
82
     *
83
     * @param  int  $id
84
     * @return \Illuminate\Http\Response
85
     */
86 6
    public function show($id)
87
    {
88
        //
89 6
        $calendar = Calendar::with('caldav')->findOrFail($id);
90 6
        $this->authorize($calendar);
91 4
        return $calendar;
92
    }
93
94
    /**
95
     * Update the specified resource in storage.
96
     *
97
     * @param  CalendarRequest  $request
98
     * @param  int  $id
99
     * @return \Illuminate\Http\Response
100
     */
101 4
    public function update(CalendarRequest $request, $id)
102
    {
103
        //
104 4
        $calendar = Calendar::findOrFail($id);
105 4
        $this->authorize($calendar);
106 4
        $input = $request->all();
107 4
        $caldav = $calendar->caldav;
108 4
        if($caldav){
109 2
            $this->validateCaldav($request);
110 2
        }
111 4
        if(isset($input['password']))
112 4
            $input['password'] = \Crypt::encrypt($input['password']);
113 4
        $calendar->update($input);
114
        //TODO test
115
        //TODO validator
116
        //TODO check if caldav exists?
117
118
        if($caldav)
119 4
            $caldav->update($input);
120 4
        return $calendar;
121
    }
122
123
    /**
124
     * Remove the specified resource from storage.
125
     *
126
     * @param  int  $id
127
     * @return \Illuminate\Http\Response
128
     */
129 2
    public function destroy($id)
130
    {
131
        //
132 2
        $calendar = Calendar::findOrFail($id);
133 2
        $this->authorize($calendar);
134 2
        $calendar->delete();
135 2
        return $calendar;
136
    }
137
138
    /**
139
     * Return a list of calendars name of a specif caldav calendar
140
     * @param Request $request
141
     * @return \Illuminate\Http\Response
142
     */
143 2
    public function getCalendars(Request $request)
144
    {
145
        //TODO VALIDATE
146
        //TODO test this
147
        try {
148 2
            $caldavClient = new SimpleCaldavAdapter();
149 2
            $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...
150
            $calendars = $caldavClient->findCalendars();
151
            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...
152 2
        }catch (\it\thecsea\caldav_client_adapter\CaldavException $e)
153
        {
154 2
            return Response::json(['error' => $e->getMessage()],422);
155
        }
156
    }
157
158
    /**
159
     * @param Request $request
160
     */
161 4
    private function validateCaldav(Request $request)
162
    {
163 4
        $this->validate($request, [
164 4
            'url' => 'required|max:255',
165 4
            'username' => 'required|max:255',
166 4
            'password' => ((\Route::current()->getName() == 'employees.calendars.caldav')?'':'sometimes|'). 'required',
167 4
            'calendar_name' => 'required|max:255',
168 4
        ]);
169 4
    }
170
}
171