-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchTaskHelper.php
More file actions
161 lines (133 loc) · 4.13 KB
/
Copy pathBatchTaskHelper.php
File metadata and controls
161 lines (133 loc) · 4.13 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?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\Map;
use Monolog\EasyGoingLogger;
use Psr\Log\LoggerInterface;
/**
* @phpstan-import-type TTaskListId from ITaskList
* @phpstan-import-type TTaskItemId from ITaskItem
*/
class BatchTaskHelper
{
/** Default key for a tasklist. */
public const string DEFAULT = 'DEFAULT';
public const bool DEFAULT_WITH_DATA_KEYS = TaskList::DEFAULT_WITH_DATA_ITEM_ID;
public const int COL_PAGEID = 0;
public const int COL_TITLE = 1;
private static bool $isInit = false;
/** @var Map<mixed,TaskList>
* @phpstan-var Map<TTaskListId,TaskList> */
protected static Map $tasklists;
protected static LoggerInterface $logger;
protected static IBatchConfig $defaultConfig;
private function __construct()
{
self::init();
}
/**
* @psalm-suppress RedundantPropertyInitializationCheck
*/
public static function init(): void
{
if (!self::$isInit) {
if (!isset(self::$logger)) {
self::$logger = EasyGoingLogger::init(BatchTaskHelper::class);
}
if (!isset(self::$tasklists)) {
self::$tasklists = new Map();
}
if (!isset(self::$defaultConfig)) {
self::$defaultConfig = new BatchConfig(new Map());
}
self::$isInit = true;
}
}
/**
* @return LoggerInterface
*/
private static function logger(): LoggerInterface
{
self::init();
return self::$logger;
}
/**
* @return Map<mixed,TaskList>
*
* @phpstan-return Map<TTaskListId,TaskList>
*/
private static function taskLists(): Map
{
self::init();
return self::$tasklists;
}
/**
* @return IBatchConfig
*/
private static function defaultConfig(): IBatchConfig
{
self::init();
return self::$defaultConfig;
}
/**
* @param mixed $listId
* @param ?IBatchConfig $listConfig
* @param bool $withHeader TRUE=with heade columns, else FALSE (only used, if tasklist will be newly created)
*
* @phpstan-param $listId TTaskListId
*
* @return TaskList
*/
public static function getTaskList(
mixed $listId = self::DEFAULT,
?IBatchConfig $listConfig = null,
bool $withHeader = self::DEFAULT_WITH_DATA_KEYS
): TaskList {
self::init();
self::logger()->debug('START - listId', [$listId]);
$listId = empty($listId) ? self::DEFAULT : $listId;
$listConfig = empty($listConfig) ? self::defaultConfig() : $listConfig;
if (!self::taskLists()->hasKey($listId)) {
self::taskLists()->put($listId, new TaskList($listId, $listConfig, $withHeader));
}
self::logger()->debug('END');
return self::taskLists()->get($listId);
}
/**
* @param string $fileName
* @param ?IBatchConfig $listConfig
* @param mixed $listId
* @param bool $withHeader TRUE=with heade columns, else FALSE
*
* @phpstan-param TTaskListId $listId
*
* @return TaskList
*/
public static function readTaskList(
string $fileName,
?IBatchConfig $listConfig = null,
mixed $listId = self::DEFAULT,
bool $withHeader = self::DEFAULT_WITH_DATA_KEYS
): TaskList {
self::init();
self::logger()->debug('START - listKey,fileName', [$listId, $fileName]);
$listId = empty($listId) ? self::DEFAULT : $listId;
if (file_exists($fileName)) {
$taskList = self::getTaskList($listId, $listConfig, $withHeader);
$taskList->readFile($fileName);
} else {
self::logger()->warning('File does not exists', [$fileName]);
$taskList = self::getTaskList($listId, $listConfig, $withHeader);
}
self::logger()->debug('END');
return $taskList;
}
}