Completed
Push — master ( c88baf...c77994 )
by claudio
08:45 queued 05:02
created

CalendarsController::storeCaldav()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4286
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Calendars;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Response;
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 30
    public function __construct()
16
    {
17 30
        config(['auth.model' => \plunner\Employee::class]);
18 30
        config(['jwt.user' => \plunner\Employee::class]);
19 30
        $this->middleware('jwt.authandrefresh:mode-en');
20 30
    }
21
22
23
    /**
24
     * Display a listing of the resource.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28 3
    public function index()
29
    {
30
        //
31
        /**
32
         * @var $employee Employee
33
         */
34 3
        $employee = \Auth::user();
35 3
        return $employee->calendars()->with('caldav')->get();
36
    }
37
38
    /**
39
     * Store a newly created resource in storage.
40
     *
41
     * @param  CalendarRequest $request
42
     * @return \Illuminate\Http\Response
43
     */
44 3
    public function store(CalendarRequest $request)
45
    {
46
        //
47 3
        $employee = \Auth::user();
48 3
        $input = $request->all();
49 3
        $calendar = $employee->calendars()->create($input);
50 3
        return $calendar;
51
    }
52
53
    /**
54
     * Store a newly created resource in storage with caldav.
55
     * <strong>CAUTION:</strong> this method returns only calendar data, not caldav
56
     *
57
     * @param  CalendarRequest $request
58
     * @return \Illuminate\Http\Response
59
     */
60 3
    public function storeCaldav(CalendarRequest $request)
61
    {
62
        //
63 3
        $this->validateCaldav($request);
64 3
        $employee = \Auth::user();
65 3
        $input = $request->all();
66 3
        $calendar = $employee->calendars()->create($input);
67 3
        if (isset($input['password']))
68 3
            $input['password'] = \Crypt::encrypt($input['password']);
69 3
        $calendar->caldav()->create($input);
70 3
        return $calendar;
71
    }
72
73
    /**
74
     * @param Request $request
75
     */
76 6
    private function validateCaldav(Request $request)
77
    {
78 6
        $this->validate($request, [
79 6
            'url' => 'required|max:255',
80 6
            'username' => 'required|max:255',
81 6
            'password' => ((\Route::current()->getName() == 'employees.calendars.caldav') ? '' : 'sometimes|') . 'required',
82 6
            'calendar_name' => 'required|max:255',
83 4
        ]);
84 6
    }
85
86
    /**
87
     * Display the specified resource.
88
     *
89
     * @param  int $id
90
     * @return \Illuminate\Http\Response
91
     */
92 9
    public function show($id)
93
    {
94
        //
95 9
        $calendar = Calendar::with('caldav')->findOrFail($id);
96 9
        $this->authorize($calendar);
97 6
        return $calendar;
98
    }
99
100
    /**
101
     * Update the specified resource in storage.
102
     *
103
     * @param  CalendarRequest $request
104
     * @param  int $id
105
     * @return \Illuminate\Http\Response
106
     */
107 6
    public function update(CalendarRequest $request, $id)
108
    {
109
        //
110 6
        $calendar = Calendar::findOrFail($id);
111 6
        $this->authorize($calendar);
112 6
        $input = $request->all();
113 6
        $caldav = $calendar->caldav;
114 6
        if ($caldav) {
115 3
            $this->validateCaldav($request);
116 2
        }
117 6
        if (isset($input['password']))
118 5
            $input['password'] = \Crypt::encrypt($input['password']);
119 6
        $calendar->update($input);
120
121 2
        if ($caldav)
122 5
            $caldav->update($input);
123 6
        return $calendar;
124
    }
125
126
    /**
127
     * Remove the specified resource from storage.
128
     *
129
     * @param  int $id
130
     * @return \Illuminate\Http\Response
131
     */
132 3
    public function destroy($id)
133
    {
134
        //
135 3
        $calendar = Calendar::findOrFail($id);
136 3
        $this->authorize($calendar);
137 3
        $calendar->delete();
138 3
        return $calendar;
139
    }
140
141
    /**
142
     * Return a list of calendars name of a specif caldav calendar
143
     * @param Request $request
144
     * @return \Illuminate\Http\Response
145
     */
146 3
    public function getCalendars(Request $request)
147
    {
148
        //TODO test this
149 3
        $this->validate($request, [
150 3
            'url' => 'required|max:255',
151 2
            'username' => 'required|max:255',
152 2
            'password' => 'required',
153 2
        ]);
154
        try {
155 3
            $caldavClient = new SimpleCaldavAdapter();
156 3
            $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...
157
            $calendars = $caldavClient->findCalendars();
158
            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...
159 3
        } catch (\it\thecsea\caldav_client_adapter\CaldavException $e) {
160 3
            return Response::json(['error' => $e->getMessage()], 422);
161
        }
162
    }
163
}
164