HomeController::getHtmlUserOutput()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace SET\Http\Controllers;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Gate;
9
use Illuminate\Support\Facades\Request;
10
use SET\Duty;
11
use SET\Handlers\Calendar\Calendar;
12
use SET\Handlers\Duty\DutyGroups;
13
use SET\Handlers\Duty\DutyUsers;
14
use SET\Training;
15
use SET\TrainingUser;
16
use SET\User;
17
18
class HomeController extends Controller
19
{
20
    /**
21
     * Display a listing of the resource.
22
     * Return notes with due dates 4 weeks from now.
23
     * Return last 20 recent notes.
24
     *
25
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
26
     */
27
    public function index()
28
    {
29
        //if you don't have view ability, redirect to your own page.
30
        if (Gate::denies('view')) {
31
            return redirect()->action('UserController@show', Auth::user()->id);
32
        }
33
34
        $trainingUser = TrainingUser::with('user', 'training')
35
            ->where('completed_date', '>=', Carbon::today()->subWeek(1))
36
            ->orderBy('updated_at', 'DESC')
37
            ->get();
38
39
        $activityLog = (new User())->getUserLog();
40
41
        $calendar = (new Calendar())->getCalendar();
42
43
        $duties = $this->getDuties();
44
45
        return view('home.index', compact('trainingUser', 'activityLog', 'calendar', 'duties'));
46
    }
47
48
    /**
49
     * Return a list of users & trainings that will be used for our ajax search bar in the headers.
50
     *
51
     * @return \Illuminate\Http\JsonResponse
52
     */
53
    public function search()
54
    {
55
        $this->authorize('view');
56
        $qInput = Request::input('q');
57
        $status = true;
58
        $dbUsers = User::skipSystem()->searchUsers($qInput)
59
            ->get(['id', 'first_name', 'last_name', 'status', 'emp_num']);
60
        $dbTraining = Training::searchTraining($qInput)->get(['id', 'name']);
61
62
        // Means no result were found
63
        if (count($dbUsers) <= 0 && count($dbTraining) <= 0) {
64
            $status = false;
65
        }
66
67
        return response()->json([
68
            'status' => $status,
69
            'error'  => null,
70
            'data'   => [
71
                'user'     => $dbUsers,
72
                'training' => $dbTraining,
73
            ],
74
        ]);
75
    }
76
77
    private function getDuties()
78
    {
79
        $newCollection = new Collection();
80
        $allDuties = Duty::all();
81
82
        foreach ($allDuties as $duty) {
83
            if ($duty->has_groups) {
84
                $userList = (new DutyGroups($duty))->getList()->first()['group'];
85
                $groupUsers = $this->getHtmlUserOutput($userList);
86
                $newCollection->push([
87
                    'duty'  => $duty->name,
88
                    'user'  => implode('; ', $groupUsers),
89
               ]);
90
            } else {
91
                $user = (new DutyUsers($duty))->getList()->first()['user'];
92
                $newCollection->push([
93
                    'duty'  => $duty->name,
94
                    'user'  => "<a href='".url('user', $user->id)."'>".$user->userFullName.'</a>',
95
                ]);
96
            }
97
        }
98
99
        return $newCollection;
100
    }
101
102
    private function getHtmlUserOutput($userList)
103
    {
104
        foreach ($userList as $user) {
105
            $groupUsers[] = "<a href='".url('user', $user->id)."'>".$user->userFullName.'</a>';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$groupUsers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $groupUsers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
106
        }
107
108
        return $groupUsers;
0 ignored issues
show
Bug introduced by
The variable $groupUsers does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
109
    }
110
}
111