Completed
Push — development ( daed94...13a157 )
by Thomas
18s
created

Importer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B import() 0 30 4
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
0 ignored issues
show
introduced by
GeoCacheService => \Oc\GeoCache\Persistence\GeoCache\GeoCacheService
Loading history...
18
     */
19
    private $geoCacheService;
20
21
    /**
22
     * @var FieldNoteService
0 ignored issues
show
introduced by
FieldNoteService => \Oc\FieldNotes\Persistence\FieldNoteService
Loading history...
23
     */
24
    private $fieldNoteService;
25
26
    /**
27
     * @param GeoCacheService $geoCacheService
0 ignored issues
show
introduced by
GeoCacheService => \Oc\GeoCache\Persistence\GeoCache\GeoCacheService
Loading history...
28
     * @param FieldNoteService $fieldNoteService
0 ignored issues
show
introduced by
FieldNoteService => \Oc\FieldNotes\Persistence\FieldNoteService
Loading history...
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
0 ignored issues
show
introduced by
ImportContext => \Oc\FieldNotes\Import\Context\ImportContext
Loading history...
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
0 ignored issues
show
introduced by
FieldNote => \Oc\FieldNotes\Struct\FieldNote
Loading history...
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