ExploreController::groupsAction()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 25
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Controllers;
13
14
use Exception;
15
use Gitamin\Facades\Setting;
16
use Gitamin\Models\Issue;
17
use Illuminate\Support\Facades\Auth;
18
use Illuminate\Support\Facades\Request;
19
use Illuminate\Support\Facades\View;
20
use Jenssegers\Date\Date;
21
22
class ExploreController extends Controller
23
{
24
    /**
25
     * Array of sub-menu items.
26
     *
27
     * @var array
28
     */
29
    protected $subMenu = [];
30
31
    /**
32
     * Creates a new project controller instance.
33
     */
34 View Code Duplication
    public function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $this->subMenu = [
37
            'yours' => [
38
                'title' => trans('dashboard.projects.yours'),
39
                'url' => route('dashboard.projects.index'),
40
                'icon' => 'fa fa-edit',
41
                'active' => false,
42
            ],
43
            'starred' => [
44
                'title' => trans('dashboard.projects.starred'),
45
                'url' => route('dashboard.projects.starred'),
46
                'icon' => 'fa fa-umbrella',
47
                'active' => false,
48
            ],
49
            'explore' => [
50
                'title' => trans('dashboard.projects.explore'),
51
                'url' => route('explore.index'),
52
                'icon' => 'fa fa-eye',
53
                'active' => false,
54
            ],
55
        ];
56
57
        View::share([
58
            'sub_menu' => $this->subMenu,
59
            'sub_title' => trans_choice('dashboard.projects.projects', 2),
60
        ]);
61
    }
62
63
    /**
64
     * Displays the explore page.
65
     *
66
     * @return \Illuminate\View\View
67
     */
68
    public function indexAction()
69
    {
70
        $this->subMenu['explore']['active'] = true;
71
72
        $today = Date::now();
73
        $startDate = Date::now();
74
75
        // Check if we have another starting date
76
        if (Request::has('start_date')) {
77
            try {
78
                // If date provided is valid
79
                $oldDate = Date::createFromFormat('Y-m-d', Request::get('start_date'));
80
81
                // If trying to get a future date fallback to today
82
                if ($today->gt($oldDate)) {
83
                    $startDate = $oldDate;
84
                }
85
            } catch (Exception $e) {
86
                // Fallback to today
87
            }
88
        }
89
90
        $daysToShow = Setting::get('app_issue_days', 0) - 1;
91
        if ($daysToShow < 0) {
92
            $daysToShow = 0;
93
            $issueDays = [];
94
        } else {
95
            $issueDays = range(0, $daysToShow);
96
        }
97
        $dateTimeZone = Setting::get('app_timezone');
98
99
        $issueVisiblity = Auth::check() ? 0 : 1;
0 ignored issues
show
Unused Code introduced by
$issueVisiblity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100
101
        $allIssues = Issue::whereBetween('created_at', [
102
            $startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00',
103
            $startDate->format('Y-m-d').' 23:59:59',
104
        ])->orderBy('created_at', 'desc')->get()->groupBy(function (Issue $issue) use ($dateTimeZone) {
105
            return (new Date($issue->created_at))
0 ignored issues
show
Documentation introduced by
The property created_at does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
                ->setTimezone($dateTimeZone)->toDateString();
107
        });
108
109
        // Add in days that have no issues
110
        foreach ($issueDays as $i) {
111
            $date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i);
112
113
            if (! isset($allIssues[$date->toDateString()])) {
114
                $allIssues[$date->toDateString()] = [];
115
            }
116
        }
117
118
        // Sort the array so it takes into account the added days
119
        $allIssues = $allIssues->sortBy(function ($value, $key) {
120
            return strtotime($key);
121
        }, SORT_REGULAR, true)->all();
122
123
        return View::make('explore.index')
124
            ->withPageTitle(trans('dashboard.explore'))
125
            ->withProjects([])
126
            ->withSubMenu($this->subMenu)
127
            ->withDaysToShow($daysToShow)
128
            ->withAllIssues($allIssues)
129
            ->withCanPageForward((bool) $today->gt($startDate))
130
            ->withCanPageBackward(Issue::where('created_at', '<', $startDate->format('Y-m-d'))->count() > 0)
131
            ->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString())
132
            ->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString());
133
    }
134
135
    public function groupsAction()
136
    {
137
        $this->subMenu = [
138
            'yours' => [
139
                'title' => trans('gitamin.groups.yours'),
140
                'url' => route('dashboard.groups.index'),
141
                'icon' => 'fa fa-edit',
142
                'active' => false,
143
            ],
144
            'explore' => [
145
                'title' => trans('gitamin.groups.explore'),
146
                'url' => route('explore.groups'),
147
                'icon' => 'fa fa-eye',
148
                'active' => false,
149
            ],
150
        ];
151
152
        $this->subMenu['explore']['active'] = true;
153
154
        return View::make('explore.groups')
155
            ->withPageTitle(trans('dashboard.explore'))
156
            ->withSubMenu($this->subMenu)
157
            ->withSubTitle(trans_choice('dashboard.groups.groups', 2))
158
            ->withProjects([]);
159
    }
160
161
    /**
162
     * Shows an issue in more detail.
163
     *
164
     * @param \Gitamin\Models\Issue $issue
165
     *
166
     * @return \Illuminate\View\View
167
     */
168
    public function showIssue(Issue $issue)
169
    {
170
        return View::make('issue')
171
            ->withIssue($issue);
172
    }
173
}
174