Issues (2963)

LibreNMS/Alert/Template.php (5 issues)

1
<?php
2
/**
3
 * Template.php
4
 *
5
 * Base Template class
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 Neil Lathwood
23
 * @author     Neil Lathwood <[email protected]>
24
 */
25
26
namespace LibreNMS\Alert;
27
28
use App\Models\AlertTemplate;
29
use LibreNMS\Enum\AlertState;
30
31
class Template
32
{
33
    public $template;
34
35
    /**
36
     * Get the template details
37
     *
38
     * @param  array|null  $obj
39
     * @return mixed
40
     */
41
    public function getTemplate($obj = null)
42
    {
43
        if ($this->template) {
44
            // Return the cached template information.
45
            return $this->template;
46
        }
47
        $this->template = AlertTemplate::whereHas('map', function ($query) use ($obj) {
48
            $query->where('alert_rule_id', '=', $obj['rule_id']);
49
        })->first();
50
        if (! $this->template) {
51
            $this->template = AlertTemplate::where('name', '=', 'Default Alert Template')->first();
52
        }
53
54
        return $this->template;
55
    }
56
57
    public function getTitle($data)
58
    {
59
        return $this->bladeTitle($data);
60
    }
61
62
    public function getBody($data)
63
    {
64
        return $this->bladeBody($data);
65
    }
66
67
    /**
68
     * Parse Blade body
69
     *
70
     * @param  array  $data
71
     * @return string
72
     */
73
    public function bladeBody($data)
74
    {
75
        $alert['alert'] = new AlertData($data['alert']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$alert was never initialized. Although not strictly required by PHP, it is generally a good practice to add $alert = array(); before regardless.
Loading history...
76
        try {
77
            return view(['template' => $data['template']->template], $alert)->__toString();
0 ignored issues
show
array('template' => $data['template']->template) of type array is incompatible with the type null|string expected by parameter $view of view(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            return view(/** @scrutinizer ignore-type */ ['template' => $data['template']->template], $alert)->__toString();
Loading history...
78
        } catch (\Exception $e) {
79
            return view(['template' => $this->getDefaultTemplate()], $alert)->__toString();
80
        }
81
    }
82
83
    /**
84
     * Parse Blade title
85
     *
86
     * @param  array  $data
87
     * @return string
88
     */
89
    public function bladeTitle($data)
90
    {
91
        $alert['alert'] = new AlertData($data['alert']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$alert was never initialized. Although not strictly required by PHP, it is generally a good practice to add $alert = array(); before regardless.
Loading history...
92
        try {
93
            return view(['template' => $data['title']], $alert)->__toString();
0 ignored issues
show
array('template' => $data['title']) of type array is incompatible with the type null|string expected by parameter $view of view(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
            return view(/** @scrutinizer ignore-type */ ['template' => $data['title']], $alert)->__toString();
Loading history...
94
        } catch (\Exception $e) {
95
            return $data['title'] ?: view(['template' => 'Template ' . $data['name']], $alert)->__toString();
0 ignored issues
show
array('template' => 'Template ' . $data['name']) of type array<string,string> is incompatible with the type null|string expected by parameter $view of view(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
            return $data['title'] ?: view(/** @scrutinizer ignore-type */ ['template' => 'Template ' . $data['name']], $alert)->__toString();
Loading history...
96
        }
97
    }
98
99
    /**
100
     * Get the default template
101
     *
102
     * @return string
103
     */
104
    public function getDefaultTemplate()
105
    {
106
        return '{{ $alert->title }}' . PHP_EOL .
107
            'Severity: {{ $alert->severity }}' . PHP_EOL .
108
            '@if ($alert->state == ' . AlertState::RECOVERED . ')Time elapsed: {{ $alert->elapsed }} @endif ' . PHP_EOL .
109
            'Timestamp: {{ $alert->timestamp }}' . PHP_EOL .
110
            'Unique-ID: {{ $alert->uid }}' . PHP_EOL .
111
            'Rule: @if ($alert->name) {{ $alert->name }} @else {{ $alert->rule }} @endif ' . PHP_EOL .
112
            '@if ($alert->faults)Faults:' . PHP_EOL .
113
            '@foreach ($alert->faults as $key => $value)' . PHP_EOL .
114
            '  #{{ $key }}: {{ $value[\'string\'] }} @endforeach' . PHP_EOL .
115
            '@endif' . PHP_EOL .
116
            'Alert sent to: @foreach ($alert->contacts as $key => $value) {{ $value }} <{{ $key }}> @endforeach';
117
    }
118
}
119