Executable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 28
c 1
b 0
f 0
dl 0
loc 107
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addJob() 0 4 1
A forUser() 0 4 1
A createCommandLine() 0 25 2
A listJobs() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of SebastianFeldmann\Crontab.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace SebastianFeldmann\Crontab\Cli;
13
14
use SebastianFeldmann\Cli\Command\Executable as CliExecutable;
15
use SebastianFeldmann\Cli\CommandLine;
16
use SebastianFeldmann\Cli\Util;
17
use SebastianFeldmann\Crontab\Job;
18
19
/**
20
 * Class Executable
21
 *
22
 * @package SebastianFeldmann\Crontab
23
 */
24
class Executable
25
{
26
    /**
27
     * Path to crontab command.
28
     *
29
     * @var string
30
     */
31
    private $cmd;
32
33
    /**
34
     * Username.
35
     *
36
     * @var string
37
     */
38
    private $user = '';
39
40
    /**
41
     * Show current crontab
42
     * -l
43
     *
44
     * @var bool
45
     */
46
    private $listJobs = false;
47
48
    /**
49
     * Job to schedule.
50
     *
51
     * @var \SebastianFeldmann\Crontab\Job
52
     */
53
    private $job;
54
55
    /**
56
     * Executable constructor.
57
     *
58
     * @param string $path
59
     */
60
    public function __construct(string $path = '')
61
    {
62
        $this->cmd = Util::detectCmdLocation('crontab', $path);
63
    }
64
65
    /**
66
     * User setter.
67
     *
68
     * @param  string $user
69
     * @return \SebastianFeldmann\Crontab\Cli\Executable
70
     */
71
    public function forUser(string $user): Executable
72
    {
73
        $this->user = $user;
74
        return $this;
75
    }
76
77
    /**
78
     *List option setter.
79
     *
80
     * @param  bool $bool
81
     * @return \SebastianFeldmann\Crontab\Cli\Executable
82
     */
83
    public function listJobs(bool $bool = true): Executable
84
    {
85
        $this->listJobs = $bool;
86
        return $this;
87
    }
88
89
    /**
90
     * Add a crontab entry to the current crontab.
91
     *
92
     * @param  \SebastianFeldmann\Crontab\Job $job
93
     * @return \SebastianFeldmann\Crontab\Cli\Executable
94
     */
95
    public function addJob(Job $job): Executable
96
    {
97
        $this->job = $job;
98
        return $this;
99
    }
100
101
    /**
102
     * Create the command line to execute the crontab commands.
103
     *
104
     * @return \SebastianFeldmann\Cli\CommandLine
105
     */
106
    public function createCommandLine(): CommandLine
107
    {
108
        $line = new CommandLine();
109
        $cmd  = new CliExecutable($this->cmd);
110
        $line->addCommand($cmd);
111
        $cmd->addOptionIfNotEmpty('-u', $this->user, true, ' ');
112
113
        if (empty($this->job)) {
114
            $cmd->addOptionIfNotEmpty('-l', $this->listJobs, false);
115
        } else {
116
            // echo current crontab
117
            $cmd->addOption('-l');
118
            // echo new job
119
            $echo = new CliExecutable('echo');
120
            $echo->addArgument((string) $this->job);
121
            $line->addCommand($echo);
122
123
            // pipe both outputs to new crontab
124
            $pipe = new CliExecutable($this->cmd);
125
            $pipe->addOptionIfNotEmpty('-u', $this->user, true, ' ')
126
                 ->addOption('-');
127
            $line->pipeOutputTo($pipe);
128
        }
129
130
        return $line;
131
    }
132
}
133