Completed
Push — master ( 1f8bb9...0c53f4 )
by Phecho
06:42 queued 03:18
created

GroupsController::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 28
Ratio 100 %
Metric Value
dl 28
loc 28
rs 8.8571
cc 1
eloc 20
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 AltThree\Validator\ValidationException;
15
use Gitamin\Commands\Owner\AddOwnerCommand;
16
use Gitamin\Commands\Owner\RemoveOwnerCommand;
17
use Gitamin\Commands\Owner\UpdateOwnerCommand;
18
use Gitamin\Models\Project;
19
use Gitamin\Models\Owner;
20
use Gitamin\Models\Group;
21
use Gitamin\Models\Tag;
22
use Gitamin\Http\Controllers\Controller;
23
use GrahamCampbell\Binput\Facades\Binput;
24
use Illuminate\Support\Facades\Redirect;
25
use Illuminate\Support\Facades\Auth;
26
use Illuminate\Support\Facades\View;
27
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
28
use Illuminate\Database\QueryException;
29
30
class GroupsController extends Controller
31
{
32
    /**
33
     * Array of sub-menu items.
34
     *
35
     * @var array
36
     */
37
    protected $subMenu = [];
38
39
    /**
40
     * Creates a new project controller instance.
41
     *
42
     * @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...
43
     */
44 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...
45
    {
46
        $this->subMenu = [
47
            'projects' => [
48
                'title'  => trans('dashboard.projects.projects'),
49
                'url'    => route('dashboard.projects.index'),
50
                'icon'   => 'fa fa-sitemap',
51
                'active' => false,
52
            ], 
53
            'groups'   => [
54
                'title'  => trans_choice('gitamin.groups.groups', 2),
55
                'url'    => route('dashboard.groups.index'),
56
                'icon'   => 'fa fa-folder',
57
                'active' => false,
58
            ],
59
            'labels' => [
60
                'title'  => trans_choice('dashboard.projects.labels.labels', 2),
61
                'url'    => route('dashboard.projects.index'),
62
                'icon'   => 'fa fa-tags',
63
                'active' => false,
64
            ],
65
        ];
66
67
        View::share([
68
            'sub_menu'  => $this->subMenu,
69
            'sub_title' => trans_choice('dashboard.projects.projects', 2),
70
        ]);
71
    }
72
73
    /**
74
     * Shows the project teams view.
75
     *
76
     * @return \Illuminate\View\View
77
     */
78 View Code Duplication
    public function indexAction()
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...
79
    {
80
        $this->subMenu['groups']['active'] = true;
81
82
        return View::make('groups.index')
83
            ->withPageTitle(trans_choice('gitamin.groups.groups', 2).' - '.trans('dashboard.dashboard'))
84
            ->withGroups(Group::get())
85
            ->withSubMenu($this->subMenu);
86
    }
87
88
     /**
89
     * Shows the group view.
90
     *
91
     * @return \Illuminate\View\View
92
     */
93
    public function showAction($path)
94
    {
95
        $group = Group::where('path', '=', $path)->first();
96
        return View::make('groups.show')
97
            ->withPageTitle($group->name)
98
            ->withGroup($group);
99
    }
100
101
    /**
102
     * Shows the new project view.
103
     *
104
     * @return \Illuminate\View\View
105
     */
106
    public function newAction()
107
    {
108
        return View::make('groups.new')
109
            ->withPageTitle(trans('dashboard.groups.new.title').' - '.trans('dashboard.dashboard'));
110
            //->withGroups(ProjectNamespace::all());
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
111
    }
112
113
    /**
114
     * Creates a new project.
115
     *
116
     * @return \Illuminate\Http\RedirectResponse
117
     */
118
    public function createAction()
119
    {
120
        $groupData = Binput::get('group');
121
        $groupData['type'] = 'group';
122
        $groupData['user_id'] = Auth::user()->id;
123
        try {
124
            $group = $this->dispatchFromArray(AddOwnerCommand::class, $groupData);
0 ignored issues
show
Unused Code introduced by
$group 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...
125
        } catch (ValidationException $e) {
126
            return Redirect::route('groups.new')
127
                ->withInput(Binput::all())
128
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.teams.add.failure')))
129
                ->withErrors($e->getMessageBag());
130
        } catch (QueryException $e) {
131
            return Redirect::route('groups.new')
132
                ->withInput(Binput::all())
133
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.teams.add.failure')))
134
                ->withErrors('Path has been used');
135
        }
136
137
        return Redirect::route('dashboard.groups.index')
138
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.teams.add.success')));
139
    }
140
141
    /**
142
     * Shows the edit project namespace view.
143
     *
144
     * @param \Gitamin\Models\ProjectNamespace $namespace
0 ignored issues
show
Bug introduced by
There is no parameter named $namespace. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
145
     *
146
     * @return \Illuminate\View\View
147
     */
148
    public function editAction($path)
149
    {
150
        $group = Group::where('path', '=', $path)->first();
151
152
        return View::make('groups.edit')
153
            ->withPageTitle(trans('dashboard.teams.edit.title').' - '.trans('dashboard.dashboard'))
154
            ->withGroup($group);
155
    }
156
157
    /**
158
     * Updates a project namespace.
159
     *
160
     * @param \Gitamin\Models\ProjectNamespace $namespace
0 ignored issues
show
Bug introduced by
There is no parameter named $namespace. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
161
     *
162
     * @return \Illuminate\Http\RedirectResponse
163
     */
164
    public function updateAction($path)
165
    {
166
        $groupData = Binput::get('group');
167
        $group = Owner::where('path', '=', $path)->first();
168
        try {
169
            $groupData['owner'] = $group;
170
            $groupData['owner_id'] = Auth::user()->id;
171
            $group = $this->dispatchFromArray(UpdateOwnerCommand::class, $groupData);
172
        } catch (ValidationException $e) {
173
            return Redirect::route('groups.group_edit', ['owner' => $group->path])
174
                ->withInput(Binput::all())
175
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('gitamin.groups.edit.failure')))
176
                ->withErrors($e->getMessageBag());
177
        }
178
179
        return Redirect::route('groups.group_edit', ['owner' => $group->path])
180
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('gitamin.groups.edit.success')));
181
    }
182
}
183