Vixie::expectUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 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\Parser;
13
14
use SebastianFeldmann\Crontab\Parser;
15
16
/**
17
 * Class Vixie
18
 * The default cron implementation for most linux distributions.
19
 * https://de.wikipedia.org/wiki/Cron
20
 *
21
 * @package SebastianFeldmann\Crontab
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/sebastianfeldmann/crontab
24
 * @since   Class available since Release 0.9.0
25
 */
26
class Vixie implements Parser
27
{
28
    /**
29
     * Should the parser look for a year value.
30
     *
31
     * @var bool
32
     */
33
    private $expectYear;
34
35
    /**
36
     * Should the parser expect a user value.
37
     *
38
     * @var bool
39
     */
40
    private $expectUser;
41
42
    /**
43
     * Look for a year value.
44
     *
45
     * @param  bool $bool
46
     * @return \SebastianFeldmann\Crontab\Parser\Vixie
47
     */
48
    public function expectYear(bool $bool = true)
49
    {
50
        $this->expectYear = $bool;
51
        return $this;
52
    }
53
54
    /**
55
     * Look for a user value.
56
     *
57
     * @param  bool $bool
58
     * @return \SebastianFeldmann\Crontab\Parser\Vixie
59
     */
60
    public function expectUser(bool $bool = true)
61
    {
62
        $this->expectUser = $bool;
63
        return $this;
64
    }
65
66
    /**
67
     * Parse a crontab command row.
68
     *
69
     * @param  string $row
70
     * @return \SebastianFeldmann\Crontab\Parser\Result
71
     */
72
    public function parse(string $row): Result
73
    {
74
        $row    = trim($row);
75
        $result = new Result();
76
        $data   = preg_split('/\s+/', $row);
77
        foreach ($this->getParts() as $index => $part) {
78
            $result->$part = $data[$index];
79
            unset($data[$index]);
80
        }
81
        // we have to keep the original whitespaces within the command
82
        $cmd    = '';
83
        $spaces = preg_split('/[^\s]+/', $row);
84
        foreach ($data as $index => $part) {
85
            $cmd .= $part . $spaces[$index + 1];
86
        }
87
        $result->command = trim($cmd);
88
89
        return $result;
90
    }
91
92
    /**
93
     * Return list of expected parts.
94
     *
95
     * @return array
96
     */
97
    protected function getParts()
98
    {
99
        $parts = ['min', 'hour', 'day', 'month', 'dayOfWeek'];
100
101
        if ($this->expectYear) {
102
            $parts[] = 'year';
103
        }
104
        if ($this->expectUser) {
105
            $parts[] = 'user';
106
        }
107
108
        return $parts;
109
    }
110
}
111