Completed
Push — develop ( f672ca...7b8dd8 )
by
unknown
07:11
created

ApiController::list_ports()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use DB;
6
use App\User;
7
use App\Device;
8
use App\Port;
9
use Illuminate\Http\Request;
10
11
class ApiController extends Controller
12
{
13
    public function __construct() {
14
15
    }
16
17
    /**
18
     * Get a list of devices
19
     */
20 View Code Duplication
    public function list_devices(Request $request) {
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...
21
        if ($request->user()->level >= 10 || $request->user()->level == 5) {
22
            return Device::all();
23
        }
24
        else {
25
            return User::find($request->user()->user_id)->devices()->get();
26
        }
27
    }
28
29
    /**
30
     * Get a list of ports
31
     */
32 View Code Duplication
    public function list_ports(Request $request) {
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...
33
        if ($request->user()->level >= 10 || $request->user()->level == 5) {
34
            return Port::all();
35
        }
36
        else {
37
            return User::find($request->user()->user_id)->ports()->get();
38
        }
39
    }
40
41
    /**
42
    * Get info about the install
43
    */
44
    public function get_info() {
45
        $versions['git'] = `git rev-parse --short HEAD`;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$versions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $versions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
46
        $versions['db_schema'] = DB::select('SELECT `version` FROM `dbSchema` LIMIT 1')[0]->version;
47
        return $versions;
48
    }
49
50
}
51