Issues (2963)

app/Models/Eventlog.php (1 issue)

1
<?php
2
/**
3
 * Eventlog.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 <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Models;
27
28
use Carbon\Carbon;
29
use Illuminate\Database\Eloquent\Relations\MorphTo;
30
use Illuminate\Support\Facades\Auth;
31
use LibreNMS\Enum\Alert;
0 ignored issues
show
This use statement conflicts with another class in this namespace, App\Models\Alert. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
32
33
class Eventlog extends DeviceRelatedModel
34
{
35
    protected $table = 'eventlog';
36
    protected $primaryKey = 'event_id';
37
    public $timestamps = false;
38
    protected $fillable = ['datetime', 'device_id', 'message', 'type', 'reference', 'username', 'severity'];
39
40
    // ---- Helper Functions ----
41
42
    /**
43
     * Log events to the event table
44
     *
45
     * @param  string  $text  message describing the event
46
     * @param  Device  $device  related device
47
     * @param  string  $type  brief category for this event. Examples: sensor, state, stp, system, temperature, interface
48
     * @param  int  $severity  1: ok, 2: info, 3: notice, 4: warning, 5: critical, 0: unknown
49
     * @param  int  $reference  the id of the referenced entity.  Supported types: interface
50
     */
51
    public static function log($text, $device = null, $type = null, $severity = Alert::INFO, $reference = null)
52
    {
53
        $log = new static([
54
            'reference' => $reference,
55
            'type' => $type,
56
            'datetime' => Carbon::now(),
57
            'severity' => $severity,
58
            'message' => $text,
59
            'username'  => (class_exists('\Auth') && Auth::check()) ? Auth::user()->username : '',
60
        ]);
61
62
        if ($device instanceof Device) {
63
            $device->eventlogs()->save($log);
64
        } else {
65
            $log->save();
66
        }
67
    }
68
69
    // ---- Define Relationships ----
70
71
    public function related(): MorphTo
72
    {
73
        return $this->morphTo('related', 'type', 'reference');
74
    }
75
}
76