1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\FieldNotes\Import; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use Oc\FieldNotes\Enum\LogType; |
8
|
|
|
use Oc\FieldNotes\Import\Context\ImportContext; |
9
|
|
|
use Oc\FieldNotes\Persistence\FieldNoteEntity; |
10
|
|
|
use Oc\FieldNotes\Persistence\FieldNoteService; |
11
|
|
|
use Oc\FieldNotes\Struct\FieldNote; |
12
|
|
|
use Oc\GeoCache\Persistence\GeoCache\GeoCacheService; |
13
|
|
|
|
14
|
|
|
class Importer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var GeoCacheService |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
private $geoCacheService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var FieldNoteService |
|
|
|
|
23
|
|
|
*/ |
24
|
|
|
private $fieldNoteService; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param GeoCacheService $geoCacheService |
|
|
|
|
28
|
|
|
* @param FieldNoteService $fieldNoteService |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
GeoCacheService $geoCacheService, |
32
|
|
|
FieldNoteService $fieldNoteService |
33
|
|
|
) { |
34
|
|
|
$this->geoCacheService = $geoCacheService; |
35
|
|
|
$this->fieldNoteService = $fieldNoteService; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Import by given context. |
40
|
|
|
* |
41
|
|
|
* @param ImportContext $context |
|
|
|
|
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function import(ImportContext $context) |
46
|
|
|
{ |
47
|
|
|
$uploadFormData = $context->getFormData(); |
48
|
|
|
$fieldNotes = $context->getFieldNotes(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var FieldNote $fieldNote |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
foreach ($fieldNotes as $fieldNote) { |
54
|
|
|
$date = new DateTime($fieldNote->noticedAt, new DateTimeZone('UTC')); |
55
|
|
|
$date->setTimezone(new DateTimeZone(date_default_timezone_get())); |
56
|
|
|
|
57
|
|
|
if ($uploadFormData->ignore && $date <= $uploadFormData->ignoreBeforeDate) { |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$geoCache = $this->geoCacheService->fetchByWaypoint( |
62
|
|
|
$fieldNote->waypoint |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$entity = new FieldNoteEntity(); |
66
|
|
|
$entity->userId = $uploadFormData->userId; |
67
|
|
|
$entity->geocacheId = $geoCache->cacheId; |
68
|
|
|
$entity->type = LogType::guess($fieldNote->logType); |
69
|
|
|
$entity->date = $date; |
70
|
|
|
$entity->text = $fieldNote->notice; |
71
|
|
|
|
72
|
|
|
$this->fieldNoteService->create($entity); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|