Completed
Pull Request — develop (#184)
by Tony
32:16
created

BaseGraph   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getGraph() 0 16 3
A createRRDXport() 0 9 1
A createRRDGraph() 0 10 1
A hasIDs() 0 4 3
A getIDs() 0 4 1
A fetchDevice() 0 9 2
A getRelation() 0 4 1
A getData() 0 4 1
getHeaders() 0 1 ?
getRRDXportDefinition() 0 1 ?
getRRDGraphDefinition() 0 1 ?
1
<?php
2
/**
3
 * BaseGraph.php
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * @package    LibreNMS
19
 * @link       http://librenms.org
20
 * @copyright  2016 Neil Lathwood
21
 * @author     Neil Lathwood <[email protected]>
22
 */
23
24
namespace App\Graphs;
25
26
use App\Data\RRDGraph;
27
use App\Data\RRDXport;
28
use App\Exceptions\UnknownDataSourceException;
29
use App\Models\Device;
30
use Illuminate\Database\Eloquent\Collection;
31
use Illuminate\Http\Request;
32
use stdClass;
33
34
abstract class BaseGraph
35
{
36
    protected $device;
37
    protected $type;
38
    protected $request;
39
    protected $input;
40
    protected $headers = [];
41
42
    /**
43
     * BaseGraph constructor.
44
     *
45
     * @param string $type
46
     * @param Request $request
47
     * @param stdClass $input
48
     */
49
    public function __construct($type, Request $request, $input = null)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
50
    {
51
        $this->type = $type;
52
        $this->request = $request;
53
        if (is_null($input)) {
54
            $this->input = json_decode($request->{'input'});
55
        } else {
56
            $this->input = $input;
57
        }
58
        $this->device = $this->fetchDevice();
59
    }
60
61
    /**
62
     * Get the data for this graph in the requested format
63
     *
64
     * @param string $format png, json, or rrd
65
     * @return string
66
     * @throws UnknownDataSourceException
67
     */
68
    public function getGraph($format)
69
    {
70
        $sourceName = $this->request->source;
71
        $source = null;
72
        if ($sourceName === 'rrd') {
73
            if ($format == 'png') {
74
                $source = $this->createRRDGraph();
75
            } else {
76
                $source = $this->createRRDXport();
77
            }
78
        } else {
79
            throw new UnknownDataSourceException("Source type $source is not supported");
80
        }
81
82
        return $source->fetch($format);
83
    }
84
85
    /**
86
     * @return RRDXport
87
     */
88
    protected function createRRDXport()
89
    {
90
        return new RRDXport(
91
            $this->getRRDXportDefinition(),
92
            $this->getHeaders(),
93
            $this->input->start,
94
            $this->input->end
95
        );
96
    }
97
98
    /**
99
     * @return RRDGraph
100
     */
101
    protected function createRRDGraph()
102
    {
103
        return new RRDGraph(
104
            $this->getRRDGraphDefinition(),
105
            $this->input->start,
106
            $this->input->end,
107
            $this->input->width,
108
            $this->input->height
109
        );
110
    }
111
112
    /**
113
     * Check if there are request specific IDs
114
     *
115
     * @return bool
116
     */
117
    protected function hasIDs()
118
    {
119
        return isset($this->input->id) && (is_numeric($this->input->id) || str_contains($this->input->id, ','));
120
    }
121
122
    /**
123
     * Get the requested ID(s)
124
     *
125
     * @return array
126
     */
127
    protected function getIDs()
128
    {
129
        return explode(',', $this->input->id);
130
    }
131
132
    /**
133
     * Fetch the device with data
134
     * We use eager loading so we can load both with one
135
     *
136
     * @return mixed
137
     */
138
    protected function fetchDevice()
139
    {
140
        if ($this->hasIDs()) {
141
            $ids = explode(',', $this->input->id);
142
            return Device::with($this->getRelation())->findMany($ids);
0 ignored issues
show
Bug introduced by
The method findMany does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
143
        }
144
145
        return Device::with($this->getRelation())->find($this->input->device_id);
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
146
    }
147
148
149
    // -- Overrride these methods --
150
151
    /**
152
     * Returns the name(s) of the relationships to load for this graph
153
     * This may also be an associative array with a closure that accepts a query object
154
     * If you do not want to load any relationships, pass an empty array
155
     *
156
     * @return string|array
157
     */
158
    protected function getRelation()
159
    {
160
        return [];
161
    }
162
163
164
    /**
165
     * Get a Collection of db data for this graph
166
     *
167
     * @return Collection
168
     */
169
    protected function getData()
170
    {
171
        return new Collection();
172
    }
173
174
    /**
175
     * Get the chart headers for this graph
176
     *
177
     * @return array
178
     */
179
    abstract protected function getHeaders();
180
181
    /**
182
     * Return the RRD definition string for Xport
183
     *
184
     * @return string
185
     */
186
    abstract protected function getRRDXportDefinition();
187
188
    /**
189
     * Return the RRD definition string for Graph creation
190
     *
191
     * @return string
192
     */
193
    abstract protected function getRRDGraphDefinition();
194
}
195