Completed
Pull Request — master (#42)
by Phecho
04:22
created

OwnerController::postOwners()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 16
loc 16
rs 9.4286
cc 2
eloc 11
nc 2
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\Api;
13
14
use Gitamin\Commands\Owner\AddOwnerCommand;
15
use Gitamin\Commands\Owner\RemoveOwnerCommand;
16
use Gitamin\Commands\Owner\UpdateOwnerCommand;
17
use Gitamin\Models\Owner;
18
use GrahamCampbell\Binput\Facades\Binput;
19
use Illuminate\Database\QueryException;
20
use Illuminate\Foundation\Bus\DispatchesJobs;
21
use Illuminate\Http\Request;
22
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
23
24
class OwnerController extends AbstractApiController
25
{
26
    use DispatchesJobs;
27
28
    /**
29
     * Get all owners.
30
     *
31
     * @param \Symfony\Component\HttpFoundation\Request $request
32
     *
33
     * @return \Illuminate\Http\JsonResponse
34
     */
35
    public function getOwners(Request $request)
36
    {
37
        $owners = Owner::paginate(Binput::get('per_page', 20));
38
39
        return $this->paginator($owners, $request);
40
    }
41
42
    /**
43
     * Get a single owner.
44
     *
45
     * @param \Gitamin\Models\Owner $owner
46
     *
47
     * @return \Illuminate\Http\JsonResponse
48
     */
49
    public function getOwner(Owner $owner)
50
    {
51
        return $this->item($owner);
52
    }
53
54
    /**
55
     * Create a new project team.
56
     *
57
     * @return \Illuminate\Http\JsonResponse
58
     */
59 View Code Duplication
    public function postOwners()
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...
60
    {
61
        try {
62
            $owner = $this->dispatch(new AddOwnerCommand(
63
                Binput::get('name'),
64
                Binput::get('path'),
65
                Binput::get('user_id'),
66
                Binput::get('description'),
67
                Binput::get('type')
68
            ));
69
        } catch (QueryException $e) {
70
            throw new BadRequestHttpException();
71
        }
72
73
        return $this->item($owner);
74
    }
75
76
    /**
77
     * Update an existing owner.
78
     *
79
     * @param \Gitamin\Models\Owner $owner
80
     *
81
     * @return \Illuminate\Http\JsonResponse
82
     */
83 View Code Duplication
    public function putOwner(Owner $owner)
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...
84
    {
85
        try {
86
            $owner = $this->dispatch(new UpdateOwnerCommand(
87
                $owner,
0 ignored issues
show
Documentation introduced by
$owner is of type object<Gitamin\Models\Owner>, but the function expects a object<Gitamin\Models\ProjectNamepsace>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
                Binput::get('name'),
89
                Binput::get('path'),
90
                Binput::get('user_id'),
91
                Binput::get('description'),
92
                Binput::get('type')
0 ignored issues
show
Unused Code introduced by
The call to UpdateOwnerCommand::__construct() has too many arguments starting with \GrahamCampbell\Binput\F...des\Binput::get('type').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93
            ));
94
        } catch (QueryException $e) {
95
            throw new BadRequestHttpException();
96
        }
97
98
        return $this->item($owner);
99
    }
100
101
    /**
102
     * Delete an existing owner.
103
     *
104
     * @param \Gitamin\Models\Owner $owner
105
     *
106
     * @return \Illuminate\Http\JsonResponse
107
     */
108
    public function deleteOwner(Owner $owner)
109
    {
110
        $this->dispatch(new RemoveOwnerCommand($owner));
111
112
        return $this->noContent();
113
    }
114
}
115