ProjectsController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 8
Bugs 3 Features 3
Metric Value
wmc 8
c 8
b 3
f 3
lcom 0
cbo 5
dl 0
loc 138
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 5 1
A newAction() 0 7 1
A showAction() 0 15 1
A editAction() 0 11 1
B createAction() 0 29 2
B updateAction() 0 24 2
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\Project\AddProjectCommand;
16
use Gitamin\Commands\Project\UpdateProjectCommand;
17
use Gitamin\Models\Group;
18
use Gitamin\Models\Owner;
19
use Gitamin\Models\Project;
20
use Gitamin\Models\Tag;
21
use Illuminate\Support\Facades\Auth;
22
use Illuminate\Support\Facades\Redirect;
23
use Illuminate\Support\Facades\Request;
24
use Illuminate\Support\Facades\View;
25
26
class ProjectsController extends Controller
27
{
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function indexAction()
34
    {
35
        //
36
        echo 'In projects controller';
37
    }
38
39
    /**
40
     * Show the form or adding a new resource.
41
     *
42
     * return \Illuminate\Http\Response
43
     */
44
    public function newAction()
45
    {
46
        return View::make('projects.new')
47
            ->withPageTitle(trans('dashboard.projects.new.title').' - '.trans('dashboard.dashboard'))
48
            ->withGroupId('')
49
            ->withOwners(Owner::where('user_id', '=', Auth::user()->id)->get());
50
    }
51
52
    /**
53
     * Show the form for creating a new resource.
54
     *
55
     * @return \Illuminate\Http\Response
56
     */
57
    public function createAction()
58
    {
59
        //
60
        $projectData = Request::get('project');
61
        $tags = array_pull($projectData, 'tags');
62
63
        try {
64
            $projectData['creator_id'] = Auth::user()->id;
65
            $project = $this->dispatchFromArray(AddProjectCommand::class, $projectData);
66
        } catch (ValidationException $e) {
67
            return Redirect::route('projects.new')
68
                ->withInput(Request::all())
69
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.new.failure')))
70
                ->withErrors($e->getMessageBag());
71
        }
72
73
        // The project was added successfully, so now let's deal with the tags.
74
        $tags = preg_split('/ ?, ?/', $tags);
75
76
        // For every tag, do we need to create it?
77
        $projectTags = array_map(function ($taggable) use ($project) {
78
            return Tag::firstOrCreate(['name' => $taggable])->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Gitamin\Models\Tag>. 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...
79
        }, $tags);
80
81
        $project->tags()->sync($projectTags);
82
83
        return Redirect::route('dashboard.projects.index')
84
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.new.success')));
85
    }
86
87
    /**
88
     * Display the specified resource.
89
     *
90
     * @param string $owner
0 ignored issues
show
Bug introduced by
There is no parameter named $owner. 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...
91
     * @param string $project_path
92
     *
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function showAction($owner_path, $project_path)
96
    {
97
        $project = Project::findByPath($owner_path, $project_path);
98
99
        return View::make('projects.show')
100
            ->withPageTitle($project->name)
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Gitamin\Models\User>. 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...
101
            ->withActiveItem('project_show')
102
            ->withProject($project)
103
            ->withRepo('')
104
            ->withRepository('')
105
            ->withCurrentBranch('master')
106
            ->withBranches([])
107
            ->withParentPath('')
108
            ->withFiles([]);
109
    }
110
111
    /**
112
     * Show the form for editing the specified resource.
113
     *
114
     * @param string $owner_path
115
     * @param string $project_path
116
     *
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function editAction($owner_path, $project_path)
120
    {
121
        $project = Project::findByPath($owner_path, $project_path);
122
123
        return View::make('projects.edit')
124
            ->withPageTitle(trans('dashboard.projects.edit.title').' - '.trans('dashboard.dashboard'))
125
            ->withProject($project)
126
            ->withGroupId('')
127
            ->withActiveItem('project_edit')
128
            ->withGroups(Group::all());
129
    }
130
131
    /**
132
     * Update the specified resource in storage.
133
     *
134
     * @param string $owner_path
135
     * @param string $project_path
136
     *
137
     * @return \Illuminate\Http\Response
138
     */
139
    public function updateAction($owner_path, $project_path)
0 ignored issues
show
Unused Code introduced by
The parameter $owner_path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $project_path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
    {
141
        $projectData = Request::get('project');
142
        $tags = array_pull($projectData, 'tags');
143
        $project = Project::find($projectData['id']);
144
        $projectData['namespace_id'] = $project->namespace_id;
145
146
        try {
147
            $projectData['project'] = $project;
148
            $projectData['creator_id'] = Auth::user()->id;
149
            $projectData['owner_id'] = $project->owner->id;
150
            $project = $this->dispatchFromArray(UpdateProjectCommand::class, $projectData);
151
        } catch (ValidationException $e) {
152
            return Redirect::route('projects.project_edit', ['owner' => $project->owner_path, 'project' => $project->path])
153
                ->withInput(Request::all())
154
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.edit.failure')))
155
                ->withErrors($e->getMessageBag());
156
        }
157
        // The project was updated successfully, so now let's deal with the tags.
158
        $tags = preg_split('/ ?, ?/', $tags);
0 ignored issues
show
Unused Code introduced by
$tags 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...
159
160
        return Redirect::route('projects.project_edit', ['owner' => $project->owner_path, 'project' => $project->path])
161
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.edit.success')));
162
    }
163
}
164