summaryrefslogtreecommitdiffstats
path: root/library/Director/Hook/JobHook.php
blob: d9a81a95f8835aa260859333e50120914fa10699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace Icinga\Module\Director\Hook;

use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorJob;
use Icinga\Module\Director\Web\Form\QuickForm;

abstract class JobHook
{
    /** @var Db */
    private $db;

    /** @var  DirectorJob */
    private $jobDefinition;

    public static function getDescription(QuickForm $form)
    {
        return false;
    }

    abstract public function run();

    public function isPending()
    {
        // TODO: Can be overridden, double-check whether this is necessary
    }

    public function setDefinition(DirectorJob $definition)
    {
        $this->jobDefinition = $definition;
        return $this;
    }

    protected function getSetting($key, $default = null)
    {
        return $this->jobDefinition->getSetting($key, $default);
    }

    public function getName()
    {
        $parts = explode('\\', get_class($this));
        $class = preg_replace('/Job$/', '', array_pop($parts));

        if (array_shift($parts) === 'Icinga' && array_shift($parts) === 'Module') {
            $module = array_shift($parts);
            if ($module !== 'Director') {
                return sprintf('%s (%s)', $class, $module);
            }
        }

        return $class;
    }

    public function exportSettings()
    {
        return $this->jobDefinition->getSettings();
    }

    public static function getSuggestedRunInterval(QuickForm $form)
    {
        return 900;
    }

    /**
     * Override this method if you want to extend the settings form
     *
     * @param  QuickForm $form QuickForm that should be extended
     * @return QuickForm
     */
    public static function addSettingsFormFields(QuickForm $form)
    {
        return $form;
    }

    public function setDb(Db $db)
    {
        $this->db = $db;
        return $this;
    }

    protected function db()
    {
        return $this->db;
    }
}