Completed
Push — datatables-fix ( e9c4b2...619c08 )
by Tony
02:59
created

ResourceController::show()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 6
eloc 12
nc 10
nop 2
1
<?php
2
/**
3
 * app/Api/Controllers/General/ResourceController.php
4
 *
5
 * API Controller for resources such as IP, ARP, Mac, etc
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Neil Lathwood
23
 * @author     Neil Lathwood <[email protected]>
24
 */
25
26
namespace App\Api\Controllers\General;
27
28
use App\Api\Controllers\Controller;
29
use App\Models\General\IPv4;
30
use App\Models\General\IPv4Mac;
31
use App\Models\General\IPv6;
32
use App\Models\Port;
33
use Dingo\Api\Http;
34
use Dingo\Api\Routing\Helpers;
35
use Illuminate\Http\Request;
36
37
class ResourceController extends Controller
38
{
39
40
    use Helpers;
41
42
    /**
43
     * Display a listing of all alerts
44
     *
45
     * @param  \Illuminate\Http\Request  $request
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function index(Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request 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...
49
    {
50
        return $this->response->array(array('statusText' => 'OK', 'resources' => ['ipv4', 'ipv6', 'arp', 'mac']));
51
    }
52
53
54
    /**
55
     * Show the form for creating a new resource.
56
     *
57
     * @return \Illuminate\Http\Response|null
58
     */
59
    public function create()
60
    {
61
        //
62
    }
63
64
    /**
65
     * Store a newly created resource in storage.
66
     *
67
     * @param  \Illuminate\Http\Request  $request
68
     * @return \Illuminate\Http\Response|null
69
     */
70
    public function store(Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request 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...
71
    {
72
        //
73
    }
74
75
    /**
76
     * Display the specified resource.
77
     *
78
     * @param Request $request
79
     * @param string $resource
80
     * @return \Illuminate\Pagination\LengthAwarePaginator
81
     */
82
    public function show(Request $request, $resource)
83
    {
84
        $per_page = $request->per_page ?: 25;
85
        if ($resource === "ipv4")
86
        {
87
            return IPv4::paginate($per_page);
88
        }
89
        elseif ($resource === "ipv6")
90
        {
91
            return IPv6::paginate($per_page);
92
        }
93
        elseif ($resource === "mac")
94
        {
95
            return Port::select('port_id', 'ifPhysAddress')->paginate($per_page);
96
        }
97
        elseif ($resource === "arp")
98
        {
99
            return IPv4Mac::paginate($per_page);
100
        }
101
        else {
102
            return response()->json(['message' => "Resource $resource not found!"], 404);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...ce} not found!"), 404); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Api\Controllers\Gene...esourceController::show of type Illuminate\Pagination\LengthAwarePaginator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
103
        }
104
    }
105
106
    /**
107
     * Show the form for editing the specified resource.
108
     *
109
     * @param  int  $id
110
     * @return \Illuminate\Http\Response|null
111
     */
112
    public function edit($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...
113
    {
114
        //
115
    }
116
117
    /**
118
     * Update the specified resource in storage.
119
     *
120
     * @param  \Illuminate\Http\Request  $request
121
     * @param  int  $id
122
     * @return \Illuminate\Http\Response|null
123
     */
124
    public function update(Request $request, $id)
1 ignored issue
show
Unused Code introduced by
The parameter $request 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 $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...
125
    {
126
        //
127
    }
128
129
    /**
130
     * Remove the specified resource from storage.
131
     *
132
     * @param  int  $id
133
     * @return \Illuminate\Http\Response|null
134
     */
135
    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...
136
    {
137
        //
138
    }
139
140
}
141