-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathAbstractSubject.php
More file actions
1604 lines (1391 loc) · 53.2 KB
/
Copy pathAbstractSubject.php
File metadata and controls
1604 lines (1391 loc) · 53.2 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* TechDivision\Import\Subjects\AbstractSubject
*
* PHP version 7
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
namespace TechDivision\Import\Subjects;
use Psr\Log\LogLevel;
use Ramsey\Uuid\Uuid;
use League\Event\EmitterInterface;
use Doctrine\Common\Collections\Collection;
use TechDivision\Import\RowTrait;
use TechDivision\Import\HeaderTrait;
use TechDivision\Import\SystemLoggerTrait;
use TechDivision\Import\Utils\ScopeKeys;
use TechDivision\Import\Utils\ColumnKeys;
use TechDivision\Import\Utils\EventNames;
use TechDivision\Import\Utils\MemberNames;
use TechDivision\Import\Utils\RegistryKeys;
use TechDivision\Import\Utils\EntityTypeCodes;
use TechDivision\Import\Utils\Generators\GeneratorInterface;
use TechDivision\Import\Callbacks\CallbackInterface;
use TechDivision\Import\Observers\ObserverInterface;
use TechDivision\Import\Interfaces\HookAwareInterface;
use TechDivision\Import\Adapter\ImportAdapterInterface;
use TechDivision\Import\Exceptions\WrappedColumnException;
use TechDivision\Import\Services\RegistryProcessorInterface;
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
/**
* An abstract subject implementation.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
abstract class AbstractSubject implements SubjectInterface, FilesystemSubjectInterface, DateConverterSubjectInterface
{
/**
* The trait that provides basic filesystem handling functionality.
*
* @var \TechDivision\Import\Subjects\FilesystemTrait
*/
use FilesystemTrait;
/**
* The trait that provides basic filesystem handling functionality.
*
* @var \TechDivision\Import\SystemLoggerTrait
*/
use SystemLoggerTrait;
/**
* The trait that provides date converting functionality.
*
* @var \TechDivision\Import\DateConverterTrait
*/
use DateConverterTrait;
/**
* The trait that provides header handling functionality.
*
* @var \TechDivision\Import\HeaderTrait
*/
use HeaderTrait;
/**
* The trait that provides row handling functionality.
*
* @var \TechDivision\Import\RowTrait
*/
use RowTrait;
/**
* The unique identifier for the actual invocation.
*
* @var string
*/
protected $uniqueId;
/**
* The name of the file to be imported.
*
* @var string
*/
protected $filename;
/**
* The actual line number.
*
* @var integer
*/
protected $lineNumber = 0;
/**
* The import adapter instance.
*
* @var \TechDivision\Import\Adapter\ImportAdapterInterface
*/
protected $importAdapter;
/**
* The subject configuration.
*
* @var \TechDivision\Import\Configuration\SubjectConfigurationInterface
*/
protected $configuration;
/**
* The plugin configuration.
*
* @var \TechDivision\Import\Configuration\PluginConfigurationInterface
*/
protected $pluginConfiguration;
/**
* The RegistryProcessor instance to handle running threads.
*
* @var \TechDivision\Import\Services\RegistryProcessorInterface
*/
protected $registryProcessor;
/**
* The actions unique serial.
*
* @var string
*/
protected $serial;
/**
* Array with the subject's observers.
*
* @var array
*/
protected $observers = array();
/**
* Array with the subject's callbacks.
*
* @var array
*/
protected $callbacks = array();
/**
* The subject's callback mappings.
*
* @var array
*/
protected $callbackMappings = array();
/**
* The available root categories.
*
* @var array
*/
protected $rootCategories = array();
/**
* The Magento configuration.
*
* @var array
*/
protected $coreConfigData = array();
/**
* The available stores.
*
* @var array
*/
protected $stores = array();
/**
* The available websites.
*
* @var array
*/
protected $storeWebsites = array();
/**
* The default store.
*
* @var array
*/
protected $defaultStore;
/**
* The store view code the create the product/attributes for.
*
* @var string
*/
protected $storeViewCode;
/**
* The UID generator for the core config data.
*
* @var \TechDivision\Import\Utils\Generators\GeneratorInterface
*/
protected $coreConfigDataUidGenerator;
/**
* UNIX timestamp with the date the last row counter has been logged.
*
* @var integer
*/
protected $lastLog = 0;
/**
* The number of the last line that has been logged with the row counter
* @var integer
*/
protected $lastLineNumber = 0;
/**
* The event emitter instance.
*
* @var \League\Event\EmitterInterface
*/
protected $emitter;
/**
* The status of the file (0 = not processed, 1 = successfully processed, 2 = processed with failure)
*
* @var array
*/
protected $status = array();
/**
* The default values for the columns from the configuration.
*
* @var array
*/
protected $defaultColumnValues = array();
/**
* The values of the actual column, pre-initialized with the default values.
*
* @var array
*/
protected $columnValues = array();
/**
* Mapping for the virtual entity type code to the real Magento 2 EAV entity type code.
*
* @var array
*/
protected $entityTypeCodeMappings = array(
EntityTypeCodes::EAV_ATTRIBUTE => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::EAV_ATTRIBUTE_SET => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_URL => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_PRICE => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_INVENTORY => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_INVENTORY_MSI => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_TIER_PRICE => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_SIMPLE => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_BUNDLE => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_VARIANT => EntityTypeCodes::CATALOG_PRODUCT,
EntityTypeCodes::CATALOG_PRODUCT_CATEGORY => EntityTypeCodes::CATALOG_CATEGORY,
EntityTypeCodes::CUSTOMER => EntityTypeCodes::CUSTOMER
);
/**
* Initialize the subject instance.
*
* @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance
* @param \TechDivision\Import\Utils\Generators\GeneratorInterface $coreConfigDataUidGenerator The UID generator for the core config data
* @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system loggers instances
* @param \League\Event\EmitterInterface $emitter The event emitter instance
*/
public function __construct(
RegistryProcessorInterface $registryProcessor,
GeneratorInterface $coreConfigDataUidGenerator,
Collection $systemLoggers,
EmitterInterface $emitter
) {
$this->emitter = $emitter;
$this->systemLoggers = $systemLoggers;
$this->registryProcessor = $registryProcessor;
$this->coreConfigDataUidGenerator = $coreConfigDataUidGenerator;
}
/**
* Return's the event emitter instance.
*
* @return \League\Event\EmitterInterface The event emitter instance
*/
public function getEmitter()
{
return $this->emitter;
}
/**
* Set's the name of the file to import
*
* @param string $filename The filename
*
* @return void
*/
public function setFilename($filename)
{
$this->filename = $filename;
}
/**
* Return's the name of the file to import.
*
* @return string The filename
*/
public function getFilename()
{
return $this->filename;
}
/**
* Set's the actual line number.
*
* @param integer $lineNumber The line number
*
* @return void
*/
public function setLineNumber($lineNumber)
{
$this->lineNumber = $lineNumber;
}
/**
* Return's the actual line number.
*
* @return integer The line number
*/
public function getLineNumber()
{
return $this->lineNumber;
}
/**
* Return's the default callback mappings.
*
* @return array The default callback mappings
*/
public function getDefaultCallbackMappings()
{
return array();
}
/**
* Load the default column values from the configuration.
*
* @return array The array with the default column values
*/
public function getDefaultColumnValues()
{
// initialize the array for the default column values
$defaultColumnValues = array();
// load the entity type from the execution context
$entityTypeCode = $this->getExecutionContext()->getEntityTypeCode();
// load the column values from the configuration
$columnValues = $this->getConfiguration()->getDefaultValues();
// query whether or not default column values for the entity type are available
if (isset($columnValues[$entityTypeCode])) {
$defaultColumnValues = $columnValues[$entityTypeCode];
}
// return the default column values
return $defaultColumnValues;
}
/**
* Load the default header mappings from the configuration.
*
* @return array The array with the default header mappings
*/
public function getDefaultHeaderMappings()
{
// initialize the array for the default header mappings
$defaultHeaderMappings = array();
// load the entity type from the execution context
$entityTypeCode = $this->getExecutionContext()->getEntityTypeCode();
// load the header mappings from the configuration
$headerMappings = $this->getConfiguration()->getHeaderMappings();
// query whether or not header mappings for the entity type are available
if (isset($headerMappings[$entityTypeCode])) {
$defaultHeaderMappings = $headerMappings[$entityTypeCode];
}
// return the default header mappings
return $defaultHeaderMappings;
}
/**
* Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'.
* If the passed value is NOT a valid date, NULL will be returned.
*
* @param string $value The value to format
*
* @return string|null The formatted date or NULL if the date is not valid
* @throws \InvalidArgumentException Is thrown, if the passed can not be formatted according to the configured date format
*/
public function formatDate($value)
{
// try to format the date according to the configured date format
$formattedDate = $this->getDateConverter()->convert($value);
// query whether or not the formatting was successufull
if ($formattedDate === null) {
throw new \InvalidArgumentException(
sprintf('Can\'t format date "%s" according given format "%s"', $value, $this->getSourceDateFormat())
);
}
// return the formatted date
return $formattedDate;
}
/**
* Extracts the elements of the passed value by exploding them
* with the also passed delimiter.
*
* @param string $value The value to extract
* @param string|null $delimiter The delimiter used to extrace the elements
*
* @return array The exploded values
*/
public function explode($value, $delimiter = null)
{
return $this->getImportAdapter()->explode($value, $delimiter);
}
/**
* Queries whether or not debug mode is enabled or not, default is FALSE.
*
* @return boolean TRUE if debug mode is enabled, else FALSE
*/
public function isDebugMode()
{
return $this->getConfiguration()->isDebugMode();
}
/**
* Queries whether or not strict mode is enabled or not, default is True.
*
* Backward compatibility
* debug = true strict = true -> isStrict == FALSE
* debug = true strict = false -> isStrict == FALSE
* debug = false strict = true -> isStrict == TRUE
* debug = false strict = false -> isStrict == FALSE
*
* @return boolean TRUE if strict mode is enabled, else FALSE
*/
public function isStrictMode()
{
return $this->getConfiguration()->isStrictMode();
}
/**
* Return's the subject's execution context configuration.
*
* @return \TechDivision\Import\Configuration\ExecutionContextInterface The execution context configuration to use
*/
public function getExecutionContext()
{
return $this->getConfiguration()->getPluginConfiguration()->getExecutionContext();
}
/**
* Set's the subject configuration.
*
* @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $configuration The subject configuration
*
* @return void
*/
public function setConfiguration(SubjectConfigurationInterface $configuration)
{
$this->configuration = $configuration;
}
/**
* Return's the subject configuration.
*
* @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* Set's the import adapter instance.
*
* @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance
*
* @return void
*/
public function setImportAdapter(ImportAdapterInterface $importAdapter)
{
$this->importAdapter = $importAdapter;
}
/**
* Return's the import adapter instance.
*
* @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance
*/
public function getImportAdapter()
{
return $this->importAdapter;
}
/**
* Return's the RegistryProcessor instance to handle the running threads.
*
* @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance
*/
public function getRegistryProcessor()
{
return $this->registryProcessor;
}
/**
* Set's the unique serial for this import process.
*
* @param string $serial The unique serial
*
* @return void
*/
public function setSerial($serial)
{
$this->serial = $serial;
}
/**
* Return's the unique serial for this import process.
*
* @return string The unique serial
*/
public function getSerial()
{
return $this->serial;
}
/**
* Merge's the passed status into the actual one.
*
* @param array $status The status to MergeBuilder
*
* @return void
*/
public function mergeStatus(array $status)
{
$this->status = array_replace_recursive($this->status, $status);
}
/**
* Retur's the actual status.
*
* @return array The actual status
*/
public function getStatus()
{
return $this->status;
}
/**
* Return's the unique identifier for the actual invocation.
*
* @return string The unique identifier
*/
public function getUniqueId()
{
return $this->uniqueId;
}
/**
* Return's the source date format to use.
*
* @return string The source date format
*/
public function getSourceDateFormat()
{
return $this->getConfiguration()->getSourceDateFormat();
}
/**
* Return's the multiple field delimiter character to use, default value is comma (,).
*
* @return string The multiple field delimiter character
*/
public function getMultipleFieldDelimiter()
{
return $this->getConfiguration()->getMultipleFieldDelimiter();
}
/**
* Return's the multiple value delimiter character to use, default value is comma (|).
*
* @return string The multiple value delimiter character
*/
public function getMultipleValueDelimiter()
{
return $this->getConfiguration()->getMultipleValueDelimiter();
}
/**
* Intializes the previously loaded global data for exactly one bunch.
*
* @param string $serial The serial of the actual import
*
* @return void
*/
public function setUp($serial)
{
// initialize the unique ID for the actual invocation
$this->uniqueId = Uuid::uuid4()->toString();
// load the status of the actual import
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
// load the global data, if prepared initially
if (isset($status[RegistryKeys::GLOBAL_DATA])) {
$this->stores = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORES];
$this->defaultStore = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::DEFAULT_STORE];
$this->storeWebsites = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORE_WEBSITES];
$this->rootCategories = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ROOT_CATEGORIES];
$this->coreConfigData = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::CORE_CONFIG_DATA];
}
// merge the header mappings with the values found in the configuration
$this->headerMappings = array_merge($this->headerMappings, $this->getDefaultHeaderMappings());
// merge the callback mappings with the mappings from the child instance
$this->callbackMappings = array_merge($this->callbackMappings, $this->getDefaultCallbackMappings());
// merge the default column values with the values found in the configuration
$this->defaultColumnValues = array_merge($this->defaultColumnValues, $this->getDefaultColumnValues());
// load the available callbacks from the configuration
$availableCallbacks = $this->getConfiguration()->getCallbacks();
// merge the callback mappings the the one from the configuration file
foreach ($availableCallbacks as $callbackMappings) {
foreach ($callbackMappings as $attributeCode => $mappings) {
// write a log message, that default callback configuration will
// be overwritten with the one from the configuration file
if (isset($this->callbackMappings[$attributeCode])) {
$this->getSystemLogger()->notice(
sprintf('Now override callback mappings for attribute %s with values found in configuration file', $attributeCode)
);
}
// override the attributes callbacks
$this->callbackMappings[$attributeCode] = $mappings;
}
}
// load the available observers
$availableObservers = $this->getObservers();
// process the observers
foreach ($availableObservers as $observers) {
// invoke the pre-import/import and post-import observers
/** @var \TechDivision\Import\Observers\ObserverInterface $observer */
foreach ($observers as $observer) {
if ($observer instanceof HookAwareInterface) {
$observer->setUp($serial);
}
}
}
}
/**
* Clean up the global data after importing the variants.
*
* @param string $serial The serial of the actual import
*
* @return void
*/
public function tearDown($serial)
{
// load the registry processor
$registryProcessor = $this->getRegistryProcessor();
// update the source directory for the next subject
foreach ($this->getStatus() as $key => $status) {
$registryProcessor->mergeAttributesRecursive($key, $status);
}
// load the available observers
$availableObservers = $this->getObservers();
// process the observers
foreach ($availableObservers as $observers) {
// invoke the pre-import/import and post-import observers
/** @var \TechDivision\Import\Observers\ObserverInterface $observer */
foreach ($observers as $observer) {
if ($observer instanceof HookAwareInterface) {
$observer->tearDown($serial);
}
}
}
// log a debug message with the new source directory
$this->getSystemLogger()->debug(
sprintf('Subject %s successfully updated status data for import %s', get_class($this), $serial)
);
}
/**
* Return's the target directory for the artefact export.
*
* @return string The target directory for the artefact export
*/
public function getTargetDir()
{
// load the status from the registry processor
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
// query whether or not a target directory (mandatory) has been configured
if (isset($status[RegistryKeys::TARGET_DIRECTORY])) {
return $status[RegistryKeys::TARGET_DIRECTORY];
}
// throw an exception if the root category is NOT available
throw new \Exception(sprintf('Can\'t find a target directory in status data for import %s', $this->getSerial()));
}
/**
* Register the passed observer with the specific type.
*
* @param \TechDivision\Import\Observers\ObserverInterface $observer The observer to register
* @param string $type The type to register the observer with
*
* @return void
*/
public function registerObserver(ObserverInterface $observer, $type)
{
// query whether or not the array with the callbacks for the
// passed type has already been initialized, or not
if (!isset($this->observers[$type])) {
$this->observers[$type] = array();
}
// append the callback with the instance of the passed type
$this->observers[$type][] = $observer;
}
/**
* Register the passed callback with the specific type.
*
* @param \TechDivision\Import\Callbacks\CallbackInterface $callback The subject to register the callbacks for
* @param string $type The type to register the callback with
*
* @return void
*/
public function registerCallback(CallbackInterface $callback, $type)
{
// query whether or not the array with the callbacks for the
// passed type has already been initialized, or not
if (!isset($this->callbacks[$type])) {
$this->callbacks[$type] = array();
}
// append the callback with the instance of the passed type
$this->callbacks[$type][] = $callback;
}
/**
* Return's the array with callbacks for the passed type.
*
* @param string $type The type of the callbacks to return
*
* @return array The callbacks
*/
public function getCallbacksByType($type)
{
// initialize the array for the callbacks
$callbacks = array();
// query whether or not callbacks for the type are available
if (isset($this->callbacks[$type])) {
$callbacks = $this->callbacks[$type];
}
// return the array with the type's callbacks
return $callbacks;
}
/**
* Return's the array with the available observers.
*
* @return array The observers
*/
public function getObservers()
{
return $this->observers;
}
/**
* Return's the array with the available callbacks.
*
* @return array The callbacks
*/
public function getCallbacks()
{
return $this->callbacks;
}
/**
* Return's the callback mappings for this subject.
*
* @return array The array with the subject's callback mappings
*/
public function getCallbackMappings()
{
return $this->callbackMappings;
}
/**
* Imports the content of the file with the passed filename.
*
*
* @param string $serial The serial of the actual import
* @param string $filename The filename to process
*
* @return void
* @throws \Exception Is thrown, if the import can't be processed
*/
public function import($serial, $filename)
{
$inProgressFilename = '';
$failedFilename = '';
try {
// initialize the serial/filename
$this->setSerial($serial);
$this->setFilename($filename);
// invoke the events that has to be fired before the artfact will be processed
$this->getEmitter()->emit(EventNames::SUBJECT_ARTEFACT_PROCESS_START, $this);
$this->getEmitter()->emit($this->getEventName(EventNames::SUBJECT_ARTEFACT_PROCESS_START), $this);
// load the system logger instance
$systemLogger = $this->getSystemLogger();
// prepare the flag filenames
$inProgressFilename = sprintf('%s.inProgress', $filename);
$importedFilename = sprintf('%s.imported', $filename);
$failedFilename = sprintf('%s.failed', $filename);
// query whether or not the file has already been imported
if ($this->isFile($failedFilename) ||
$this->isFile($importedFilename) ||
$this->isFile($inProgressFilename)
) {
// log a debug message and exit
$systemLogger->debug(sprintf('Import running, found inProgress file "%s"', $inProgressFilename));
return;
}
// flag file as in progress
$this->touch($inProgressFilename);
// track the start time
$startTime = microtime(true);
// initialize the last time we've logged the counter with the processed rows per minute
$this->lastLog = time();
// log a message that the file has to be imported
$systemLogger->info(
sprintf('Now start processing file "%s"', basename($filename)),
array('operation-name' => $operationName = $this->getFullOperationName())
);
// let the adapter process the file
$this->getImportAdapter()->import(array($this, 'importRow'), $filename);
// track the time needed for the import in seconds
$endTime = microtime(true) - $startTime;
// log a message that the file has successfully been imported,
// use log level warning ONLY if rows have been skipped
$systemLogger->log(
$this->getSkippedRows() > 0 ? LogLevel::WARNING : LogLevel::NOTICE,
sprintf(
'Successfully processed file "%s" with "%d" lines (skipping "%d") in "%f" s',
basename($filename),
$this->getLineNumber() - 1,
$this->getSkippedRows(),
$endTime
),
array('operation-name' => $operationName)
);
// rename flag file, because import has been successfull
if ($this->getConfiguration()->isCreatingImportedFile()) {
$this->rename($inProgressFilename, $importedFilename);
} else {
$this->getFilesystemAdapter()->delete($inProgressFilename);
}
// update the status
$this->mergeStatus(
array(
RegistryKeys::STATUS => array(
RegistryKeys::FILES => array(
$filename => array(
$this->getUniqueId() => array(
RegistryKeys::STATUS => 1,
RegistryKeys::SKIPPED_ROWS => $this->getSkippedRows(),
RegistryKeys::PROCESSED_ROWS => $this->getLineNumber() - 1
)
)
)
)
)
);
// invoke the events that has to be fired when the artfact has been successfully processed
$this->getEmitter()->emit(EventNames::SUBJECT_ARTEFACT_PROCESS_SUCCESS, $this);
$this->getEmitter()->emit($this->getEventName(EventNames::SUBJECT_ARTEFACT_PROCESS_SUCCESS), $this);
} catch (\Exception $e) {
// rename the flag file, because import failed and write the stack trace
$this->rename($inProgressFilename, $failedFilename);
$this->write($failedFilename, $e->__toString());
// update the status with the error message
$this->mergeStatus(
array(
RegistryKeys::STATUS => array(
RegistryKeys::FILES => array(
$filename => array(
$this->getUniqueId() => array(
RegistryKeys::STATUS => 2,
RegistryKeys::ERROR_MESSAGE => $e->getMessage(),
RegistryKeys::SKIPPED_ROWS => $this->getSkippedRows(),
RegistryKeys::PROCESSED_ROWS => $this->getLineNumber() - 1
)
)
)
)
)
);
// invoke the events that has to be fired when the artfact can't be processed
$this->getEmitter()->emit(EventNames::SUBJECT_ARTEFACT_PROCESS_FAILURE, $this, $e);
$this->getEmitter()->emit($this->getEventName(EventNames::SUBJECT_ARTEFACT_PROCESS_FAILURE), $this, $e);
// do not wrap the exception if not already done
if ($e instanceof WrappedColumnException) {
throw $e;
}
// else wrap and throw the exception
throw $this->wrapException(array(), $e);
}
}
/**
* Imports the passed row into the database. If the import failed, the exception
* will be catched and logged, but the import process will be continued.
*
* @param array $row The row with the data to be imported
*
* @return void
*/
public function importRow(array $row)
{
// initialize the row
$this->row = $row;
// raise the line number and reset the skip row flag
$this->lineNumber++;