Passed
Pull Request — development (#682)
by Nick
07:18
created

Importer::import()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 3
nop 1
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
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
/**
15
 * Class Importer
16
 *
17
 * @package Oc\FieldNotes\Import
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
18
 */
19
class Importer
20
{
21
    /**
22
     * @var GeoCacheService
0 ignored issues
show
introduced by
GeoCacheService => \Oc\GeoCache\Persistence\GeoCache\GeoCacheService
Loading history...
23
     */
24
    private $geoCacheService;
25
26
    /**
27
     * @var FieldNoteService
0 ignored issues
show
introduced by
FieldNoteService => \Oc\FieldNotes\Persistence\FieldNoteService
Loading history...
28
     */
29
    private $fieldNoteService;
30
31
    /**
32
     * Importer constructor.
33
     *
34
     * @param GeoCacheService $geoCacheService
0 ignored issues
show
introduced by
GeoCacheService => \Oc\GeoCache\Persistence\GeoCache\GeoCacheService
Loading history...
35
     * @param FieldNoteService $fieldNoteService
0 ignored issues
show
introduced by
FieldNoteService => \Oc\FieldNotes\Persistence\FieldNoteService
Loading history...
36
     */
37
    public function __construct(
38
        GeoCacheService $geoCacheService,
39
        FieldNoteService $fieldNoteService
40
    ) {
41
        $this->geoCacheService = $geoCacheService;
42
        $this->fieldNoteService = $fieldNoteService;
43
    }
44
45
    /**
46
     * Import by given context.
47
     *
48
     * @param ImportContext $context
0 ignored issues
show
introduced by
ImportContext => \Oc\FieldNotes\Import\Context\ImportContext
Loading history...
49
     *
50
     * @return void
51
     */
52
    public function import(ImportContext $context)
53
    {
54
        $uploadFormData = $context->getFormData();
55
        $fieldNotes = $context->getFieldNotes();
56
57
        /**
58
         * @var FieldNote $fieldNote
0 ignored issues
show
introduced by
FieldNote => \Oc\FieldNotes\Struct\FieldNote
Loading history...
59
         */
60
        foreach ($fieldNotes as $fieldNote) {
61
            $date = new DateTime($fieldNote->noticedAt, new DateTimeZone('UTC'));
62
            $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
63
64
            if ($uploadFormData->ignore && $date <= $uploadFormData->ignoreBeforeDate) {
65
                continue;
66
            }
67
68
            $geoCache = $this->geoCacheService->fetchByWaypoint(
69
                $fieldNote->waypoint
70
            );
71
72
            $entity = new FieldNoteEntity();
73
            $entity->userId = $uploadFormData->userId;
74
            $entity->geocacheId = $geoCache->cacheId;
75
            $entity->type = LogType::guess($fieldNote->logType);
76
            $entity->date = $date;
77
            $entity->text = $fieldNote->notice;
78
79
            $this->fieldNoteService->create($entity);
80
        }
81
    }
82
}
83