-
Notifications
You must be signed in to change notification settings - Fork 9
/
ImportService.php
386 lines (350 loc) · 11.7 KB
/
ImportService.php
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
use \In\Parsers\SheetRecordParserAbstract;
use \Config\Config;
use \Transform\EntityPopulator\EntityPopulator;
use \In\Parsers\RecordDefinition\RecordDefinition;
use Transform\EntityPopulator\Helper\EntityPersistenceHelper;
/**
* Manages the Client instantiation and runs the import process.
*
* @author Juan Manuel Fernandez <juanmf@gmail.com>
*/
class ImportService
{
/**
* The default absolute file path to the file containing the sheet to be imported
*/
const DEFAULT_DATA_FILE = '/tmp/datos.csv';
/**
* The default sheet name config to be used to interpret data in input sheet file.
* {@see Config/config.yml}:
* <pre>
* sheets:
* demandas: #sheetName
*/
const CONFIG_SHEET = 'demandas';
/**
* The default record name config to be used to interpret data in input sheet file.
* {@see Config/config.yml}:
* <pre>
* sheets:
* demandas: #sheetName
* records:
* demanda: #record name
*/
const RECORDTYPE = 'demanda';
/**
* The default file Format to be used, its case sensitive as the parser class name
* beggins with this string and ends with 'SheetRecordParser' i.e. 'CsvSheetRecordParser'.
*/
const FILETYPE = 'Csv';
/**
* The default sheet field delimiter, for CSV.
*/
const DELIMITER = ';';
/**
* file where we stroe a serialized array with just created Ids. in case we want to
* delete them, change something in config and try again.
*/
const LAST_CREATED_ISSUES_IDS_FNAME = '/lastIssuesId.serialized';
/**
* File where er write out a print_r of the entities that had errors and the error message.
*/
const ERROR_FILE_NAME = "/errors.dump";
/**
* The singleton
*
* @var self
*/
private static $instance = null;
/**
* @var SheetRecordParserAbstract
*/
private $parser;
/**
* @var \Redmine\Client
*/
private $client;
/**
* Import process Config parameters follows
* @var string
*/
private $dataFileName;
private $sheet;
private $delimiter;
private $file_type;
private $record;
/**
* Used to retrieve the rigth set of createdIds, for issue deletion
* @var string
*/
private $currentProject;
/**
* Singleton implementation
*
* @param string $fileName The absolute pat to data csv file
*
* @return ImportService an instance of self
*/
public static function getInstance(
$fileName = self::DEFAULT_DATA_FILE, $sheet = self::CONFIG_SHEET,
$delimiter = null, $fileType = self::FILETYPE, $record = self::RECORDTYPE
) {
if (null === self::$instance) {
self::$instance = new self($fileName, $sheet, $delimiter, $fileType, $record);
}
return self::$instance;
}
/**
* Initializes API client and configuration from {@link Config/config.yml}.
*
* @param string $fileName {@see self::DEFAULT_DATA_FILE}
* @param string $sheet {@see self::CONFIG_SHEET}
* @param string $delimiter {@see self::DELIMITER}
* @param string $fileType {@see self::FILETYPE}
* @param string $record {@see self::RECORDTYPE}
*
* @return void
*/
protected function __construct(
$fileName = self::DEFAULT_DATA_FILE, $sheet = self::CONFIG_SHEET,
$delimiter = null, $fileType = self::FILETYPE, $record = self::RECORDTYPE
) {
/**
* TODO: MANDAR ESTO AL OUT.
*/
$redmineAccount = Config::get('redmine_account');
$host = $redmineAccount['host'];
$key = $redmineAccount['api_key'];
$this->client = new Redmine\Client($host, $key);
$this->dataFileName = $fileName;
$this->sheet = $sheet;
$this->delimiter = $delimiter;
$this->file_type = $fileType;
$this->record = $record;
}
/**
* Initialize the sheet parser.
*
* @return void
*/
public function initImporter()
{
$input_format = Config::get('input_format');
$impData = array (
'sheet' => $this->sheet,
'delimiter' => $this->delimiter ? : $input_format['delimiter'],
'file_type' => $this->file_type,
'record' => $this->record,
);
$file = array('tmp_name' => $this->dataFileName);
$this->setCurrentProject();
$this->parser = $this->configureImport($impData, $file);
}
/**
* Handles Creation of SheetRecordParserAbstract
*
* @param array $impData Import config data as given by the user.
* {@see SelectSheetForm}
* @param array $file The File, with the sheet data. $fileName['tmp_name']
*
* @return array Numeric indexed, with a an instance of
* SheetRecordParserAbstract in index 0 and an instance of sfImportSheetRecordForm
* in index 1. array(SheetRecordParserAbstract, sfImportSheetRecordForm)
*/
private function configureImport($impData, $file)
{
$sheets = Config::get('sheets');
$recordDef = $sheets[$impData['sheet']]['records'][$impData['record']];
$persistenceEngine = $sheets[$impData['sheet']]['persistence_engine'];
$record = RecordDefinition::getInstance($recordDef, $persistenceEngine);
$recordParser = SheetRecordParserAbstract::getInstance(
$file, $impData['file_type'], $record, $impData['delimiter']
);
return $recordParser;
}
/**
* uses EntityPopulator to parse csv sheet and create redmine API entities (issue, user, etc)
* and persist them through the API.
*
* @return void
*/
public function executeCreate()
{
$this->initImporter();
Transform\Transformers\Transformer::unSerializeMappings();
EntityPopulator::populateEntities($this->parser);
$this->serializeLastCreatedIds();
$this->checkForErrors();
Transform\Transformers\Transformer::serializeMappings();
}
/**
* uses EntityPopulator to parse csv sheet and create redmine API entities (issue, user, etc)
* and persist them through the API.
*
* @return void
*/
public function executeUpdate()
{
$this->initImporter();
Transform\Transformers\Transformer::unSerializeMappings();
EntityPopulator::populateEntities($this->parser);
$this->serializeLastCreatedIds();
$this->checkForErrors();
Transform\Transformers\Transformer::serializeMappings();
}
/**
* Checks if the entityPopulator registered any errors, if so dumps them in
* a file called __DIR__ . {@link self::ERROR_FILE_NAME}
*
* @return void
*/
private function checkForErrors()
{
// TODO: MAKE THIS WORK WITH ANY PERSISTENCE ENGINE
$erroeFileName = __DIR__ . self::ERROR_FILE_NAME;
$conflicts = EntityPersistenceHelper::getConflicts();
if (0 < $count = count($conflicts['Transform\EntityPopulator\Entities\Issue'])) {
file_put_contents($erroeFileName, print_r(($conflicts), true));
echo sprintf('check For errors in %s, %s found' . PHP_EOL, $erroeFileName, $count);
}
}
/**
* Throws an exception if expcted condition doesn't apply.
*
* @param type $value
* @param type $expected
*
* @throws Excetion
*/
protected function assertExpected($value, $expected, $message)
{
$throw = false;
switch (true) {
case is_object($expected):
if (null !== $value && ! ($value instanceof $expected)) {
$throw = true;
}
break;
case is_array($expected):
if (null !== $value && ! is_array($value)) {
$throw = true;
}
break;
case is_scalar($expected):
if (null !== $value && ! is_scalar($value)) {
$throw = true;
}
break;
}
if ($throw) {
throw new Exception($message);
}
}
/**
* Deletes all issues in the given project.
*
* @param string $projectIdentifier The Redmine project identifier
*
* @return void
*/
public function deleteIssuesInProject($projectIdentifier)
{
$issueApi = $this->client->api('issue');
/* @var $issueApi \Redmine\Api\Issue */
while (true) {
$issues = $issueApi->all(array('project_id' => $projectIdentifier, 'limit' => 100, 'status_id' => '*'));
$this->assertExpected($issues, array(), sprintf('I Expected an Array, "%s" given.', print_r($issues, true)));
if (0 === $issues['total_count']) {
break;
}
$this->deleteIssueList($issues, $issueApi);
}
}
/**
* Deletes all issues in the given project.
*
* @param string $projectIdentifier The Redmine project identifier
*
* @return void
*/
public function deleteLastRunCreatedIssues($progectidentifier)
{
$issues = $this->unSerializeLastCreatedIds();
$this->assertExpected(
$issues[$progectidentifier], array(),
sprintf('I Expected an Array, "%s" given.', print_r($issues, true))
);
$this->deleteIssueList($issues[$progectidentifier]);
}
/**
* iterates over a list of issues and deletes them using their Ids.
*
* @param array $issues The Issue List
* @param \Redmine\Api\Issue $issueApi The Issue Api object, optional.
*
* @return void
*/
private function deleteIssueList(array $issues, \Redmine\Api\Issue $issueApi = null)
{
$issueApi = $issueApi ? : $this->client->api('issue');
/* @var $issueApi \Redmine\Api\Issue */
foreach ($issues['issues'] as $issue) {
$issueApi->remove($issue['id']);
}
}
/**
* Tries to find in __DIR__ a file with name lastIssuesId.serialized which should
* contain an array of generated Issues Id of a previous run.
*
* @return array The list of Issues Id, created in last Run.
*/
protected function unSerializeLastCreatedIds()
{
$savedIds = __DIR__ . self::LAST_CREATED_ISSUES_IDS_FNAME;
if (! file_exists($savedIds)) {
return;
}
$serialized = file_get_contents($savedIds);
return unserialize($serialized);
}
/**
* Serializes this run created Ids in __DIR__ with name lastIssuesId.serialized
*
* @return void
*/
protected function serializeLastCreatedIds()
{
$ids = \Transform\EntityPopulator\Entities\Issue::$createdIds;
$fName = __DIR__ . self::LAST_CREATED_ISSUES_IDS_FNAME;
file_put_contents($fName, serialize($ids));
}
// <editor-fold defaultstate="collapsed" desc="Accessors&Mutators">
public function getParser()
{
return $this->parser;
}
public function setParser(SheetRecordParserAbstract $parser)
{
$this->parser = $parser;
}
public function getClient()
{
return $this->client;
}
public function setClient(\Redmine\Client $client)
{
$this->client = $client;
}
public function getCurrentProject()
{
return $this->currentProject;
}
public function setCurrentProject()
{
$sheets = Config::get('sheets');
$this->currentProject = $sheets[$this->sheet]['records'][$this->record]['project_id'];
ini_set('xdebug.var_display_max_data', '99999');
}
// </editor-fold>
}