Completed
Pull Request — master (#39)
by Phecho
05:53
created

ProjectsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 1
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 GrahamCampbell\Binput\Facades\Binput;
22
use Illuminate\Support\Facades\Auth;
23
use Illuminate\Support\Facades\Redirect;
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
            ->withGroups(Owner::Mine()->get());
50
    }
51
52
    /**
53
     * Show the form for creating a new resource.
54
     *
55
     * @return \Illuminate\Http\Response
56
     */
57 View Code Duplication
    public function createAction()
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...
58
    {
59
        //
60
        $projectData = Binput::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(Binput::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
91
     * @param string $project_path
92
     *
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function showAction($owner, $project_path)
96
    {
97
        $project = Project::leftJoin('owners', function ($join) {
98
            $join->on('projects.owner_id', '=', 'owners.id');
99
        })->where('projects.path', '=', $project_path)->where('owners.path', '=', $owner)->first(['projects.*']);
100
101
        return View::make('projects.show')
102
            ->withPageTitle($project->name)
103
            ->withActiveItem('project_show')
104
            ->withProject($project)
105
            ->withRepo('')
106
            ->withRepository('')
107
            ->withCurrentBranch('master')
108
            ->withBranches([])
109
            ->withParentPath('')
110
            ->withFiles([]);
111
    }
112
113
    /**
114
     * Show the form for editing the specified resource.
115
     *
116
     * @param string $namespace
117
     * @param string $project_path
118
     *
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function editAction($namespace, $project_path)
122
    {
123
        $project = Project::leftJoin('owners', function ($join) {
124
            $join->on('projects.owner_id', '=', 'owners.id');
125
        })->where('owners.path', '=', $project_path)->where('owners.path', '=', $namespace)->first(['projects.*']);
126
127
        return View::make('projects.edit')
128
            ->withPageTitle(trans('dashboard.projects.new.title').' - '.trans('dashboard.dashboard'))
129
            ->withProject($project)
130
            ->withGroupId('')
131
            ->withGroups(Group::all());
132
    }
133
134
    /**
135
     * Update the specified resource in storage.
136
     *
137
     * @param string $namespace
138
     * @param string $project_path
139
     *
140
     * @return \Illuminate\Http\Response
141
     */
142
    public function updateAction($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...
143
    {
144
        $projectData = Binput::get('project');
145
        $tags = array_pull($projectData, 'tags');
146
        $project = Project::find($projectData['id']);
147
        $projectData['namespace_id'] = $project->namespace_id;
148
149
        try {
150
            $projectData['project'] = $project;
151
            $projectData['creator_id'] = Auth::user()->id;
152
            $project = $this->dispatchFromArray(UpdateProjectCommand::class, $projectData);
153
        } catch (ValidationException $e) {
154
            return Redirect::route('projects.project_edit', ['namespace' => $project->namespace, 'project' => $project->path])
155
                ->withInput(Binput::all())
156
                ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.edit.failure')))
157
                ->withErrors($e->getMessageBag());
158
        }
159
        // The project was updated successfully, so now let's deal with the tags.
160
        $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...
161
162
        return Redirect::route('projects.project_edit', ['namespace' => $project->namespace, 'project' => $project->path])
163
            ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.edit.success')));
164
    }
165
}
166