Completed
Push — master ( 437106...78f9b5 )
by Andreas
31:22
created

initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.baseclasses
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * This is the base class used for all jobs run by MidCOM CRON.
13
 *
14
 * It gives you an easy to use way of building cron jobs. You should rely only on
15
 * the two event handlers _on_initialize and execute, which are called by the
16
 * cron service.
17
 *
18
 * A simple (and useless) handler class would look like this:
19
 *
20
 * <code>
21
 * <?php
22
 * class net_nehmer_static_cron_test extends midcom_baseclasses_components_cron_handler
23
 * {
24
 *     function _on_initialize()
25
 *     {
26
 *         return true;
27
 *     }
28
 *
29
 *     function execute()
30
 *     {
31
 *         $this->print_error("Executing...");
32
 *         $this->print_error(strftime('%x %X'));
33
 *     }
34
 * }
35
 * </code>
36
 *
37
 * <b>Cron Job implementation suggestions</b>
38
 *
39
 * You should keep output to stdout to an absolute minimum. Normally, no output whatsoever
40
 * should be made, as the cron service itself is invoked using some kind of Cron Daemon. Only
41
 * if you output nothing, no status mail will be generated by cron.
42
 *
43
 * @see midcom_services_cron
44
 * @package midcom.baseclasses
45
 */
46
abstract class midcom_baseclasses_components_cron_handler extends midcom_baseclasses_components_base
47
{
48
    /**
49
     * @var OutputInterface
50
     */
51
    private $output;
52
53
    /**
54
     * Initialize the cron job. Before calling the on_initialize callback, it prepares
55
     * the instance with various configuration variables
56
     */
57 1
    public function initialize(OutputInterface $output)
58
    {
59 1
        $this->output = $output;
60
61 1
        return $this->_on_initialize();
62
    }
63
64
    /**
65
     * This callback is executed immediately after object construction. You can initialize your class here.
66
     * If you return false here, the handler is not executed, the system skips it.
67
     *
68
     * All class members are already initialized when this event handler is called.
69
     *
70
     * @return boolean Returns true, if initialization was successful, false to abort execution
71
     */
72 1
    public function _on_initialize()
73
    {
74 1
        return true;
75
    }
76
77
    /**
78
     * This is the actual handler operation, it is called only after successful operation.
79
     * You should use the print_error() helper of this class in case you need to notify
80
     * the user of any errors. As long as everything goes fine, you should not print anything
81
     * to avoid needless cron mailings.
82
     */
83
    abstract public function execute();
84
85
    /**
86
     * Echo the error message to the client, automatically appending
87
     * the classname to the prefix. Passed messages are also written to the error log.
88
     *
89
     * @param string $message The error message to print.
90
     * @param mixed $var A variable you want to print, if any.
91
     */
92
    public function print_error(string $message, $var = null)
93
    {
94
        $prefix = 'ERROR';
95
        if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) {
96
            $prefix .= ' ' . get_class($this);
97
        }
98
        $this->output->writeln("$prefix: $message");
99
        debug_add($message, MIDCOM_LOG_ERROR);
100
        if ($var !== null) {
101
            debug_print_r('Passed argument: ', $var);
102
        }
103
    }
104
}
105