Completed
Push — master ( bd1aa5...f7ad55 )
by claudio
05:29
created

CalendarsController::validateCaldav()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6667
cc 2
eloc 6
nc 1
nop 1
crap 6
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;
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
        $calendar->caldav()->create($input);
70
        //TODO test
71
        //TODO validator
72
        //TODO return caldav info
73
        //TODO supprot function to create simple calendar
74
        return $calendar;
75
    }
76
77
    /**
78
     * Display the specified resource.
79
     *
80
     * @param  int  $id
81
     * @return \Illuminate\Http\Response
82
     */
83 6
    public function show($id)
84
    {
85
        //
86 6
        $calendar = Calendar::with('caldav')->findOrFail($id);
87 6
        $this->authorize($calendar);
88 4
        return $calendar;
89
    }
90
91
    /**
92
     * Update the specified resource in storage.
93
     *
94
     * @param  CalendarRequest  $request
95
     * @param  int  $id
96
     * @return \Illuminate\Http\Response
97
     */
98 2
    public function update(CalendarRequest $request, $id)
99
    {
100
        //
101 2
        $calendar = Calendar::findOrFail($id);
102 2
        $this->authorize($calendar);
103 2
        $input = $request->all();
104 2
        $caldav = $calendar->caldav;
105
        if($caldav)
106 2
            $this->validateCaldav($request);
107 2
        $calendar->update($input);
108
        //TODO test
109
        //TODO validator
110
        //TODO check if caldav exists?
111
112
        if($caldav)
113 2
            $caldav->update($input);
114 2
        return $calendar;
115
    }
116
117
    /**
118
     * Remove the specified resource from storage.
119
     *
120
     * @param  int  $id
121
     * @return \Illuminate\Http\Response
122
     */
123 2
    public function destroy($id)
124
    {
125
        //
126 2
        $calendar = Calendar::findOrFail($id);
127 2
        $this->authorize($calendar);
128 2
        $calendar->delete();
129 2
        return $calendar;
130
    }
131
132
    /**
133
     * Return a list of calendars name of a specif caldav calendar
134
     * @param Request $request
135
     * @return \Illuminate\Http\Response
136
     */
137
    public function getCalendars(Request $request)
138
    {
139
        //TODO VALIDATE
140
        //TODO test this
141
        try {
142
            $caldavClient = new SimpleCaldavAdapter();
143
            $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...
144
            $calendars = $caldavClient->findCalendars();
145
            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...
146
        }catch (\it\thecsea\caldav_client_adapter\CaldavException $e)
147
        {
148
            return Response::json(['error' => $e->getMessage()],422);
149
        }
150
    }
151
152
    /**
153
     * @param Request $request
154
     */
155
    private function validateCaldav(Request $request)
156
    {
157
        $this->validate($request, [
158
            'url' => 'required|active_url|max:255',
159
            'username' => 'required|max:255',
160
            'password' => ((\Route::current()->getName() == 'companies.calendars.caldav')?'required|':'').'confirmed',
161
            'calendar_name' => 'required|max:255',
162
        ]);
163
    }
164
}
165