Completed
Pull Request — develop (#28)
by Neil
07:35
created

ApiController::get_stats()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
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
    /**
14
    * Get info about the install
15
    */
16
    public function get_info()
0 ignored issues
show
Coding Style introduced by
get_info uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
17
    {
18
        $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...
19
        $versions['db_schema'] = DB::select('SELECT `version` FROM `dbSchema` LIMIT 1')[0]->version;
20
        $versions['php'] = phpversion();
21
        $versions['mysql'] = DB::select('SELECT version() AS version')[0]->version;
22
        if (empty($_SERVER['SERVER_SOFTWARE'])) {
23
            $_SERVER['SERVER_SOFTWARE'] = "";
24
        }
25
        $versions['apache'] = str_replace('Apache/', '', $_SERVER['SERVER_SOFTWARE']);
26
        return $versions;
27
    }
28
29
    /**
30
     * Get statistics about the install
31
    **/
32
    public function get_stats()
33
    {
34
        $stats['devices']    = Device::all()->count();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$stats was never initialized. Although not strictly required by PHP, it is generally a good practice to add $stats = 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...
35
        $stats['ports']      = Port::all()->count();
36
        $stats['syslog']     = DB::select('SELECT COUNT(seq) AS `total` FROM `syslog`')[0]->total;
37
        $stats['eventlog']   = DB::select('SELECT COUNT(event_id) AS `total` FROM `eventlog`')[0]->total;
38
        $stats['apps']       = DB::select('SELECT COUNT(app_id) AS `total` FROM `applications`')[0]->total;
39
        $stats['services']   = DB::select('SELECT COUNT(service_id) AS `total` FROM `services`')[0]->total;
40
        $stats['storage']    = DB::select('SELECT COUNT(storage_id) AS `total` FROM `storage`')[0]->total;
41
        $stats['diskio']     = DB::select('SELECT COUNT(diskio_id) AS `total` FROM `ucd_diskio`')[0]->total;
42
        $stats['processors'] = DB::select('SELECT COUNT(processor_id) AS `total` FROM `processors`')[0]->total;
43
        $stats['memory']     = DB::select('SELECT COUNT(mempool_id) AS `total` FROM `mempools`')[0]->total;
44
        $stats['sensors']    = DB::select('SELECT COUNT(sensor_id) AS `total` FROM `sensors`')[0]->total;
45
        $stats['toner']      = DB::select('SELECT COUNT(toner_id) AS `total` FROM `toner`')[0]->total;
46
        $stats['hrmib']      = DB::select('SELECT COUNT(hrDevice_id) AS `total` FROM `hrDevice`')[0]->total;
47
        $stats['entmib']     = DB::select('SELECT COUNT(entPhysical_id) AS `total` FROM `entPhysical`')[0]->total;
48
        $stats['ipv4_addr']  = DB::select('SELECT COUNT(ipv4_address_id) AS `total` FROM `ipv4_addresses`')[0]->total;
49
        $stats['ipv4_net']   = DB::select('SELECT COUNT(ipv4_network_id) AS `total` FROM `ipv4_networks`')[0]->total;
50
        $stats['ipv6_addr']  = DB::select('SELECT COUNT(ipv6_address_id) AS `total` FROM `ipv6_addresses`')[0]->total;
51
        $stats['ipv6_net']   = DB::select('SELECT COUNT(ipv6_network_id) AS `total` FROM `ipv6_networks`')[0]->total;
52
        $stats['pw']         = DB::select('SELECT COUNT(pseudowire_id) AS `total` FROM `pseudowires`')[0]->total;
53
        $stats['vrf']        = DB::select('SELECT COUNT(vrf_id) AS `total` FROM `vrfs`')[0]->total;
54
        $stats['vlans']      = DB::select('SELECT COUNT(vlan_id) AS `total` FROM `vlans`')[0]->total;
55
        return $stats;
56
    }
57
58
}
59