Completed
Push — master ( 906f5a...709e36 )
by Phecho
03:29
created

DashboardController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
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 Gitamin\Facades\Setting;
15
use Gitamin\Models\Issue;
16
use Gitamin\Models\Project;
17
use Gitamin\Models\Subscriber;
18
use Illuminate\Routing\Controller;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gitamin\Http\Controllers\Controller.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use Illuminate\Support\Facades\View;
20
use Jenssegers\Date\Date;
21
22
class DashboardController extends Controller
23
{
24
    /**
25
     * Start date.
26
     *
27
     * @var \Jenssegers\Date\Date
28
     */
29
    protected $startDate;
30
31
    /**
32
     * The timezone the system is running in.
33
     *
34
     * @var string
35
     */
36
    protected $timeZone;
37
38
    /**
39
     * Creates a new dashboard controller.
40
     *
41
     * @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...
42
     */
43
    public function __construct()
44
    {
45
        $this->startDate = new Date();
46
        $this->dateTimeZone = Setting::get('app_timezone');
0 ignored issues
show
Bug introduced by
The property dateTimeZone does not seem to exist. Did you mean timeZone?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
    }
48
49
    /**
50
     * Shows the dashboard view.
51
     *
52
     * @return \Illuminate\View\View
53
     */
54
    public function showDashboard()
55
    {
56
        $projects = Project::orderBy('order')->get();
57
        //var_dump($projects);
58
        //$projects = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
        $issues = $this->getIssues();
60
        $subscribers = $this->getSubscribers();
61
62
        return View::make('dashboard.index')
63
            ->withPageTitle(trans('dashboard.dashboard'))
64
            ->withProjects($projects)
65
            ->withIssues($issues)
66
            ->withSubscribers($subscribers);
67
    }
68
69
    /**
70
     * Shows the notifications view.
71
     *
72
     * @return \Illuminate\View\View
73
     */
74
    public function showNotifications()
75
    {
76
        return View::make('dashboard.notifications.index')
77
            ->withPageTitle(trans('dashboard.notifications.notifications').' '.trans('dashboard.dashboard'));
78
    }
79
80
    /**
81
     * Fetches all of the issues over the last 30 days.
82
     *
83
     * @return \Illuminate\Support\Collection
84
     */
85 View Code Duplication
    protected function getIssues()
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...
86
    {
87
        $allIssues = Issue::whereBetween('created_at', [
88
            $this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
89
            $this->startDate->format('Y-m-d').' 23:59:59',
90
        ])->orderBy('created_at', 'desc')->get()->groupBy(function (Issue $issue) {
91
            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...
92
                ->setTimezone($this->dateTimeZone)->toDateString();
0 ignored issues
show
Bug introduced by
The property dateTimeZone does not seem to exist. Did you mean timeZone?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
93
        });
94
95
        // Add in days that have no issues
96
        foreach (range(0, 30) as $i) {
97
            $date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i);
0 ignored issues
show
Bug introduced by
The property dateTimeZone does not seem to exist. Did you mean timeZone?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
98
99
            if (!isset($allIssues[$date->toDateString()])) {
100
                $allIssues[$date->toDateString()] = [];
101
            }
102
        }
103
104
        // Sort the array so it takes into account the added days
105
        $allIssues = $allIssues->sortBy(function ($value, $key) {
106
            return strtotime($key);
107
        }, SORT_REGULAR, false);
108
109
        return $allIssues;
110
    }
111
112
    /**
113
     * Fetches all of the subscribers over the last 30 days.
114
     *
115
     * @return \Illuminate\Support\Collection
116
     */
117 View Code Duplication
    protected function getSubscribers()
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...
118
    {
119
        $allSubscribers = Subscriber::whereBetween('created_at', [
120
            $this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
121
            $this->startDate->format('Y-m-d').' 23:59:59',
122
        ])->orderBy('created_at', 'desc')->get()->groupBy(function (Subscriber $issue) {
123
            return (new Date($issue->created_at))
0 ignored issues
show
Documentation introduced by
The property created_at does not exist on object<Gitamin\Models\Subscriber>. 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...
124
                ->setTimezone($this->dateTimeZone)->toDateString();
0 ignored issues
show
Bug introduced by
The property dateTimeZone does not seem to exist. Did you mean timeZone?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
125
        });
126
127
        // Add in days that have no issues
128
        foreach (range(0, 30) as $i) {
129
            $date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i);
0 ignored issues
show
Bug introduced by
The property dateTimeZone does not seem to exist. Did you mean timeZone?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
130
131
            if (!isset($allSubscribers[$date->toDateString()])) {
132
                $allSubscribers[$date->toDateString()] = [];
133
            }
134
        }
135
136
        // Sort the array so it takes into account the added days
137
        $allSubscribers = $allSubscribers->sortBy(function ($value, $key) {
138
            return strtotime($key);
139
        }, SORT_REGULAR, false);
140
141
        return $allSubscribers;
142
    }
143
}
144