Access::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
1
<?php
2
3
namespace JeroenG\GuestPass\Controllers;
4
5
use JeroenG\GuestPass\GuestPassFacade as GuestPass;
6
use Illuminate\Foundation\Bus\DispatchesJobs;
7
use Illuminate\Routing\Controller as BaseController;
8
use Illuminate\Foundation\Validation\ValidatesRequests;
9
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
10
11
class Access extends BaseController
12
{
13
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
14
15
    public function __invoke($owner, $key)
16
    {
17
        // First, get the Guest Pass and corresponding object models.
18
        $pass = GuestPass::getGuestPass($key);
19
        $object = (new $pass->object_model)->findOrFail($pass->object_id);
20
21
        // Check if there is a view handle saved in the database.
22
        if ($pass->view == null) {
23
            // If not, see if a view named after the object model exists.
24
            $viewName = strtolower(class_basename($object));
25
            if (file_exists(resource_path('views/guests/'.$viewName.'.blade.php'))) {
26
                return view('guests/'.$viewName, ['object' => $object]);
27
            }
28
        // If there is a view handle saved, check if the file exists.
29
        } elseif (file_exists(resource_path('views/guests/'.$pass->view.'.blade.php'))) {
30
            return view('guests/'.$$pass->view, ['object' => $object, 'guestpass' => $pass]);
31
        }
32
        // In case neither exists, abort.
33
        abort(404);
34
    }
35
}
36