HomeController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Account;
6
use App\Group;
7
use App\ProxyListItem;
8
use App\User;
9
use App\Category;
10
11
class HomeController extends Controller
12
{
13
    /**
14
     * Create a new controller instance.
15
     *
16
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
23
    /**
24
     * Show the application dashboard.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function index()
29
    {
30
        return view('home',
31
            [
32
                'accounts' => $this->accountSummary(),
33
                'groups' => $this->groupSummary(),
34
                'items' => $this->listSummary(),
35
                'users' => $this->userSummary(),
36
            ]
37
        );
38
    }
39
40 View Code Duplication
    private function accountSummary()
41
    {
42
        $list = [ 'total' => 0, 'summary' => []];
43
        $categories = Category::orderBy('name', 'asc')->get();
44
        foreach ($categories as $category) {
45
            $count = Account::where('category_id', $category->id)->count();
46
            if ($count) {
47
                $list['summary'][]= [
48
                    'count' => $count,
49
                    'text' => $category->name
50
                ];
51
                $list['total'] += $count;
52
            }
53
        }
54
        return $list;
55
    }
56
57 View Code Duplication
    private function groupSummary()
58
    {
59
        $list = [ 'total' => 0, 'summary' => []];
60
        $groups = Group::orderBy('name', 'asc')->get();
61
        foreach ($groups as $group) {
62
            $count = Account::where('group_id', $group->id)->count();
63
            $list['summary'][]= [
64
                'count' => $count,
65
                'text' => $group->name
66
            ];
67
        }
68
        $list['total'] = $groups->count();
69
        return $list;
70
    }
71
72
    private function listSummary()
73
    {
74
        $list = [ 'total' => 0, 'summary' => [] ];
75
        $types = [ 'domain', 'url' ];
76
        foreach ($types as $type) {
77
            $count = ProxyListItem::where('type', $type)->count();
78
            if ($count) {
79
                $list['summary'][]= [
80
                    'count' => $count,
81
                    'text' => "{$type}s"
82
                ];
83
                $list['total'] += $count;
84
            }
85
        }
86
        return $list;
87
    }
88
89
    private function userSummary()
90
    {
91
        $list = [ 'total' => 0, 'summary' => []];
92
        foreach (User::USER_LEVEL as $id => $level) {
93
            $count = User::where('level', $id)->count();
94
            if ($count) {
95
                $list['summary'][]= [
96
                    'count' => $count,
97
                    'text' => $level['text']
98
                ];
99
                $list['total'] += $count;
100
            }
101
        }
102
        return $list;
103
    }
104
}
105