Completed
Push — develop ( 0c5671...41fe6b )
by Seth
01:57
created

AutoCrontabJob::__construct()   C

Complexity

Conditions 8
Paths 26

Size

Total Lines 62
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 6.943
c 0
b 0
f 0
cc 8
eloc 37
nc 26
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/** AutoCrontabJob and related classes */
4
5
namespace Battis;
6
7
/**
8
 * An object-oriented approach to scheduling regular cron jobs automatically
9
 *
10
 * @author Seth Battis <[email protected]>
11
 **/
12
abstract class AutoCrontabJob
13
{
14
    /** @var \TiBeN\CrontabManager\CrontabRepository Crontab management */
15
    private $cron = null;
16
17
    /** @var \Log A Pear Log handle */
18
    protected $log = null;
19
20
    /**
21
     * Construct a new AutoCrontabJob
22
     *
23
     * @param string $identifier Hopefully unique!
24
     * @param string $script Path to data collection script (probably `__FILE__`)
25
     * @param string|TiBeN\CrontabManager\CrontabJob Cron schedule or a complete
26
     *        `TiBeN\CrontabManager\CrontabJob`
27
     * @param string (Optional) path to log file (defaults to same directory and
28
     *        name as the script file) e.g. a script at `/var/www/foo/bar.php` would
29
     *        have a log at `/var/www/foo/bar.log`
30
     *
31
     * @throws AutoCrontabJob_Exception CONSTRUCTOR_ERROR If parameters are not validated
32
     **/
33
    public function __construct($identifier, $script, $schedule, $log = null)
34
    {
35
        /* Make sure the scheduled script file exists... */
36
        if (file_exists($script)) {
37
            /* try to make the identifier truly unique to this instance */
38
            $_identifier  = $identifier . '.' . md5($identifier . __FILE__ . __CLASS__);
39
40
            /* ensure that we're working with a valid Cron job */
41
            $newJob = null;
0 ignored issues
show
Unused Code introduced by
$newJob is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
42
            if (is_string($schedule)) {
43
                $newJob = \TiBeN\CrontabManager\CrontabJob::createFromCrontabLine($schedule . " php $script");
44
            } elseif ($schedule instanceof \TiBeN\CrontabManager\CrontabJob) {
45
                $newJob = $schedule;
46
            } else {
47
                throw new AutoCrontabJobException(
48
                    'Expected a string or TiBeN\CrontabManager\CrontabJob, received ' . print_r($schedule, true),
49
                    AutoCrontabJobException::CONSTRUCTOR_ERROR
50
                );
51
            }
52
            $newJob->comments = implode(' ', [
53
                $newJob->comments,
54
                "Created by $script:" . get_class($this) . ' ' . date('Y-m-d h:ia') . " (Job ID $_identifier)"
55
            ]);
56
57
58
            /* update cron if this job already exists */
59
            $this->cron = new \TiBeN\CrontabManager\CrontabRepository(new \TiBeN\CrontabManager\CrontabAdapter());
60
            $jobs = $this->cron->getJobs();
61
            $jobExists = false;
62
            // TODO Rewrite whenever this is fixed: https://github.com/TiBeN/CrontabManager/issues/3
63
            foreach ($jobs as $job) {
64
                if (preg_match("/$_identifier/", $job->formatCrontabLine())) {
65
                    $jobExists = true;
66
                    $job->minutes = $newJob->minutes;
67
                    $job->hours = $newJob->hours;
68
                    $job->dayOfMonth = $newJob->dayOfMonth;
69
                    $job->months = $newJob->months;
70
                    $job->dayOfWeek = $newJob->dayOfWeek;
71
                    break;
72
                }
73
            }
74
75
            /* ... or add this as a new job if it doesn't exist */
76
            if (!$jobExists) {
77
                $this->cron->addJob($newJob);
78
            }
79
80
            /* set up log file */
81
            if (empty($log)) {
82
                $log = dirname($script) . '/' . basename($script, '.php') . '.log';
83
            }
84
            $this->log = \Log::singleton('file', $log);
85
86
            /* update cron to enable scheduled jobs */
87
            $this->cron->persist();
88
        } else {
89
            throw new AutoCrontabJobException(
90
                "PHP script '$script' does not exist",
91
                AutoCrontabJobException::CONSTRUCTOR_ERROR
92
            );
93
        }
94
    }
95
96
    /**
97
     * Scheduled job
98
     *
99
     * Override this method to do something scheduled by Cron.
100
     *
101
     * @return void
102
     **/
103
    abstract public function scheduledJob();
104
}
105