Test Failed
Pull Request — develop (#200)
by Tony
15:03
created

WidgetDataFactory::createById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 9.4285
1
<?php
2
/**
3
 * WidgetDataFactory.php
4
 *
5
 * -Description-
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  2017 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Data;
27
28
use App\Data\Interfaces\WidgetDataInterface;
29
use App\Data\Widgets\NullWidget;
30
use App\Models\Widgets;
31
32
class WidgetDataFactory
33
{
34
    /**
35
     * @param int $id The id from the widgets table
36
     * @return WidgetDataInterface
37
     */
38
    public static function createById($id)
39
    {
40
        $name = Widgets::find($id)->widget;
41
        $class = str_replace(' ', '', ucwords(str_replace('-', ' ', $name)));
42
        $fqclass = 'App\Data\Widgets\\'.$class;
43
44
        if (class_exists($fqclass)) {
45
            return new $fqclass;
46
        }
47
48
        return new NullWidget();
49
    }
50
}
51