Completed
Pull Request — master (#17)
by Phecho
42:37
created

ProjectsController::new()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Gitamin\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Foundation\Bus\DispatchesJobs;
7
use Illuminate\Support\Facades\Redirect;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\View;
10
use GrahamCampbell\Binput\Facades\Binput;
11
use AltThree\Validator\ValidationException;
12
use Gitamin\Commands\Project\AddProjectCommand;
13
use Gitamin\Commands\Project\RemoveProjectCommand;
14
use Gitamin\Commands\Project\UpdateProjectCommand;
15
use Gitamin\Models\Project;
16
use Gitamin\Models\ProjectNamespace;
17
use Gitamin\Models\Group;
18
use Gitamin\Models\Tag;
19
20
use Gitamin\Http\Controllers\Controller;
21
22
class ProjectsController extends Controller
23
{
24
    use DispatchesJobs;
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index()
31
    {
32
        //
33
        echo "In projects controller";
34
    }
35
36
    /**
37
    * Show the form or adding a new resource.
38
    *
39
    * return \Illuminate\Http\Response
40
    */
41
    public function new()
42
    {
43
        return View::make('projects.new')
44
            ->withPageTitle(trans('dashboard.projects.new.title').' - '.trans('dashboard.dashboard'))
45
            ->withGroupId('')
46
            ->withGroups(Group::Mine()->get());
47
    }
48
49
    /**
50
     * Show the form for creating a new resource.
51
     *
52
     * @return \Illuminate\Http\Response
53
     */
54
    public function create()
55
    {
56
        //
57
        $projectData = Binput::get('project');
58
        $tags = array_pull($projectData, 'tags');
59
60
        try {
61
            $projectData['creator_id'] = Auth::user()->id;
62
            $project = $this->dispatchFromArray(AddProjectCommand::class, $projectData);
63
        } catch (ValidationException $e) {
64
            return Redirect::route('projects.new')
65
                ->withInput(Binput::all())
66
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.new.failure')))
67
                ->withErrors($e->getMessageBag());
68
        }
69
70
        // The project was added successfully, so now let's deal with the tags.
71
        $tags = preg_split('/ ?, ?/', $tags);
72
73
        // For every tag, do we need to create it?
74
        $projectTags = array_map(function ($taggable) use ($project) {
75
            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...
76
        }, $tags);
77
78
        $project->tags()->sync($projectTags);
79
80
        return Redirect::route('dashboard.projects.index')
81
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.new.success')));
82
    }
83
84
    /**
85
     * Display the specified resource.
86
     *
87
     * @param  string  $namespace
88
     * @param  string  $project_path
89
     * @return \Illuminate\Http\Response
90
     */
91
    public function show($namespace, $project_path)
92
    {
93
        $project = Project::leftJoin('namespaces', function($join) {
94
            $join->on('projects.namespace_id', '=', 'namespaces.id');
95
        })->where('projects.path', '=', $project_path)->where('namespaces.path', '=', $namespace)->first(['projects.*']);
96
97
        return View::make('projects.show')
98
            ->withPageTitle($project->name)
99
            ->withProject($project)
100
            ->withRepo('')
101
            ->withRepository('')
102
            ->withCurrentBranch('master')
103
            ->withBranches([])
104
            ->withParentPath('')
105
            ->withFiles([]);
106
    }
107
108
    /**
109
     * Show the form for editing the specified resource.
110
     *
111
     * @param  int  $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function edit($namespace, $project_path)
115
    {
116
        $project = Project::leftJoin('namespaces', function($join) {
117
            $join->on('projects.namespace_id', '=', 'namespaces.id');
118
        })->where('projects.path', '=', $project_path)->where('namespaces.path', '=', $namespace)->first(['projects.*']);
119
        
120
        return View::make('projects.edit')
121
            ->withPageTitle(trans('dashboard.projects.new.title').' - '.trans('dashboard.dashboard'))
122
            ->withProject($project)
123
            ->withGroupId('')
124
            ->withGroups(Group::all());
125
    }
126
127
    /**
128
     * Update the specified resource in storage.
129
     *
130
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. 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...
131
     * @param  int  $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
132
     * @return \Illuminate\Http\Response
133
     */
134
    public function update($namespace, $project_path)
0 ignored issues
show
Unused Code introduced by
The parameter $namespace 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...
135
    {
136
        $projectData = Binput::get('project');
137
        $tags = array_pull($projectData, 'tags');
138
        $project = Project::find($projectData['id']);
139
        $projectData['namespace_id'] = $project->namespace_id;
140
141
        try {
142
            $projectData['project'] = $project;
143
            $project = $this->dispatchFromArray(UpdateProjectCommand::class, $projectData);
144
        } catch (ValidationException $e) {
145
            return Redirect::route('projects.project_edit', ['namespace' => $project->namespace, 'project' => $project->path])
146
                ->withInput(Binput::all())
147
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.edit.failure')))
148
                ->withErrors($e->getMessageBag());
149
        }
150
        // The project was updated successfully, so now let's deal with the tags.
151
        $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...
152
153
        return Redirect::route('projects.project_edit', ['namespace' => $project->namespace, 'project' => $project->path])
154
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.edit.success')));
155
    }
156
157
    /**
158
     * Remove the specified resource from storage.
159
     *
160
     * @param  int  $id
161
     * @return \Illuminate\Http\Response
162
     */
163
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
164
    {
165
        //
166
    }
167
}
168