Completed
Pull Request — develop (#184)
by Tony
23:35
created

GraphController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A json() 0 6 1
A png() 0 5 1
A csv() 0 6 1
A initializeData() 0 13 2
1
<?php
2
/**
3
 * app/Api/Controllers/GraphController.php
4
 *
5
 * API Controller for alerts log data
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;
27
28
use App\Graphs\BaseGraph;
29
use App\Models\Device;
30
use Dingo\Api\Routing\Helpers;
31
use Illuminate\Http\Request;
32
33
class GraphController extends Controller
34
{
35
36
    use Helpers;
37
38
    /**
39
     * Obtain and format data for json output
40
     *
41
     * @param Request $request
42
     * @param string $type
43
     * @return \Illuminate\Pagination\LengthAwarePaginator
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45
    public function json(Request $request, $type)
46
    {
47
        ob_start('ob_gzhandler');
48
        $data = $this->initializeData($request, $type);
49
        return $data->json();
50
    }
51
52
    /**
53
     * Obtain and format data for png output
54
     *
55
     * @param Request $request
56
     * @param string $type
57
     * @return \Illuminate\Pagination\LengthAwarePaginator
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
58
     */
59
    public function png(Request $request, $type)
60
    {
61
        $data = $this->initializeData($request, $type);
62
        return $data->png();
63
    }
64
65
    /**
66
     * Obtain and format data for csv output
67
     *
68
     * @param Request $request
69
     * @param string $type
70
     * @return \Illuminate\Pagination\LengthAwarePaginator
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
71
     */
72
    public function csv(Request $request, $type)
73
    {
74
        ob_start('ob_gzhandler');
75
        $data = $this->initializeData($request, $type);
76
        return $data->csv();
77
    }
78
79
    /**
80
     * Common initialization
81
     *
82
     * @param Request $request
83
     * @param string $type
84
     * @return BaseGraph
85
     * @throws \Exception
86
     */
87
    private function initializeData(Request $request, $type)
88
    {
89
        $class = 'App\Graphs\\'.ucfirst($type);
90
        if (!class_exists($class)) {
91
            throw new \Exception("Graph type $type ($class) not found");
92
        }
93
94
        $input = json_decode($request->{'input'});
95
        $device = Device::find($input->device_id);
96
        /** @var BaseGraph $data */
97
        $data = new $class($device, $type, $request, $input);
98
        return $data;
99
    }
100
}
101