ListFormatter::format()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace SebastianFeldmann\Crontab\Output;
4
5
use SebastianFeldmann\Cli\Command\OutputFormatter;
6
use SebastianFeldmann\Crontab\Job;
7
use SebastianFeldmann\Crontab\Parser;
8
9
class ListFormatter implements OutputFormatter
10
{
11
    protected const ROW_TYPE_EMPTY   = 1;
12
    protected const ROW_TYPE_COMMENT = 2;
13
    protected const ROW_TYPE_COMMAND = 4;
14
15
    /**
16
     * Buffer of comments for a command row.
17
     *
18
     * @var array
19
     */
20
    private $commentBuffer;
21
22
    /**
23
     * List of jobs.
24
     * @var \SebastianFeldmann\Crontab\Job[]
25
     */
26
    private $entries;
27
28
    /**
29
     * @var \SebastianFeldmann\Crontab\Parser
30
     */
31
    private $parser;
32
33
    /**
34
     * ListFormatter constructor.
35
     *
36
     * @param \SebastianFeldmann\Crontab\Parser $parser
37
     */
38
    public function __construct(Parser $parser)
39
    {
40
        $this->parser = $parser;
41
    }
42
43
    /**
44
     * Format the output.
45
     *
46
     * @param  array $output
47
     * @return iterable
48
     */
49
    public function format(array $output)
50
    {
51
        $this->clearCommentBuffer();
52
        $this->entries = [];
53
54
        foreach ($output as $row) {
55
            $this->handleRow($row);
56
        }
57
58
        return $this->entries;
59
    }
60
61
    /**
62
     * Check row type and process row.
63
     *
64
     * @param string $row
65
     */
66
    protected function handleRow(string $row)
67
    {
68
        switch ($this->getRowType($row)) {
69
            case self::ROW_TYPE_EMPTY:
70
                $this->clearCommentBuffer();
71
                break;
72
            case self::ROW_TYPE_COMMENT:
73
                $this->commentBuffer[] = $row;
74
                break;
75
            case self::ROW_TYPE_COMMAND:
76
                $this->entries[] = $this->createEntry($row);
77
                break;
78
        }
79
    }
80
81
    /**
82
     * Determine the row type.
83
     *   - empty
84
     *   - comment
85
     *   - command
86
     *
87
     * @param  string $row
88
     * @return int
89
     */
90
    private function getRowType(string $row)
91
    {
92
        // remove leading and trailing whitespaces
93
        $row = trim($row);
94
        // empty is empty is empty
95
        if (empty($row)) {
96
            return self::ROW_TYPE_EMPTY;
97
        }
98
        // comment lines should start with a '#'
99
        if (substr($row, 0, 1) === '#') {
100
            return self::ROW_TYPE_COMMENT;
101
        }
102
        // row is not empty and doesn't start with a #
103
        // should be a command line
104
        return self::ROW_TYPE_COMMAND;
105
    }
106
107
    /**
108
     * Reset the comment buffer.
109
     */
110
    protected function clearCommentBuffer()
111
    {
112
        $this->commentBuffer = [];
113
    }
114
115
    /**
116
     * Return current comment buffer and resets it.
117
     *
118
     * @return array
119
     */
120
    protected function flushCommentBuffer(): array
121
    {
122
        $buffer = $this->commentBuffer;
123
        $this->clearCommentBuffer();
124
        return $buffer;
125
    }
126
127
    /**
128
     * Creates a crontab job from a crontab row string.
129
     *
130
     * @param  string $row
131
     * @return \SebastianFeldmann\Crontab\Job
132
     */
133
    protected function createEntry(string $row): Job
134
    {
135
        $result = $this->parser->parse($row);
136
        return new Job($result->getSchedule(), $result->command, $this->flushCommentBuffer());
137
    }
138
}
139