-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskList.php
More file actions
143 lines (117 loc) · 3.73 KB
/
Copy pathTaskList.php
File metadata and controls
143 lines (117 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/*
* This file is part of ezkoding
*
* (c) 2025 Oliver Glowa, coding.glowa.com
*
* This source file is subject to the Apache-2.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace ollily\Tools\Batch;
use Ds\Queue;
use Monolog\EasyGoingLogger;
use Psr\Log\LoggerInterface;
class TaskList
{
private static LoggerInterface $logger;
private string $listKey;
/** @var Queue<ITaskItem> */
private Queue $tasks;
public function __construct(string $listKey)
{
self::$logger = EasyGoingLogger::init(TaskList::class);
$this->listKey = $listKey;
$this->tasks = new Queue();
}
public function getListKey(): string
{
return $this->listKey;
}
public function addTask(ITaskItem $task): void
{
$this->tasks->push($task);
}
public function nextTask(): ?ITaskItem
{
$task = null;
if (!$this->isEmpty()) {
$task = $this->tasks->pop();
}
return $task;
}
public function count(): int
{
return $this->tasks->count();
}
public function isEmpty(): bool
{
return $this->tasks->isEmpty();
}
public function readFile(string $fileName): bool
{
self::$logger->debug('START - fileName', [$fileName]);
$fileRead = false;
if (!empty($fileName)) {
$fHandle = fopen($fileName, 'r');
if (is_resource($fHandle)) {
$idx = 0;
while ($line = fgets($fHandle, 1000)) {
$convertedLine = mb_convert_encoding($line, 'UTF-8');
$itemKey = $this->listKey . $idx;
$newTask = $this->parseTaskData($itemKey, $convertedLine);
if (!is_null($newTask)) {
$this->addTask($newTask);
$idx++;
}
}
fclose($fHandle);
$fileRead = true;
}
}
self::$logger->debug('END');
return $fileRead;
}
protected function parseTaskData(mixed $itemKey, mixed $convertedLine): ?ITaskItem
{
self::$logger->debug('START - itemKey', [$itemKey]);
$newTask = null;
if (is_string($convertedLine)) {
$newLine = preg_filter("/(\r|\n|\r\n)/", '', $convertedLine);
self::$logger->debug('newLine', [$newLine]);
/** @psalm-suppress RiskyTruthyFalsyComparison */
if (!empty($newLine)) {
$newTask = new TaskItem($itemKey, explode(';', $newLine));
}
}
self::$logger->debug('newTask', [$newTask]);
self::$logger->debug('END');
return $newTask;
}
public function storeFile(string $fileName): bool
{
self::$logger->debug('START - fileName', [$fileName]);
$fileStored = false;
if (!empty($fileName)) {
$fHandle = fopen($fileName, 'w');
if (is_resource($fHandle)) {
while (!$this->isEmpty()) {
$line = $this->nextTask() ?? '';
if ($line instanceof ITaskItem) {
$line = $line->__toString();
}
$convertedLine = mb_convert_encoding($line, 'UTF-8');
/** @phpstan-ignore notIdentical.alwaysTrue */
if ($convertedLine !== false) {
$convertedLine .= "\n";
fwrite($fHandle, $convertedLine);
}
}
fclose($fHandle);
$fileStored = true;
}
}
self::$logger->debug('END');
return $fileStored;
}
}