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

UploadFormDataFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\FieldNotes\Form;
4
5
use DateTime;
6
use Oc\FieldNotes\Persistence\FieldNoteService;
7
use Oc\GeoCache\Persistence\GeoCacheLog\GeoCacheLogService;
8
9
class UploadFormDataFactory
10
{
11
    /**
12
     * @var FieldNoteService
0 ignored issues
show
introduced by
FieldNoteService => \Oc\FieldNotes\Persistence\FieldNoteService
Loading history...
13
     */
14
    private $fieldNoteService;
15
16
    /**
17
     * @var GeoCacheLogService
0 ignored issues
show
introduced by
GeoCacheLogService => \Oc\GeoCache\Persistence\GeoCacheLog\GeoCacheLogService
Loading history...
18
     */
19
    private $geoCacheLogService;
20
21
    /**
22
     * @param FieldNoteService $fieldNoteService
0 ignored issues
show
introduced by
FieldNoteService => \Oc\FieldNotes\Persistence\FieldNoteService
Loading history...
23
     * @param GeoCacheLogService $geoCacheLogService
0 ignored issues
show
introduced by
GeoCacheLogService => \Oc\GeoCache\Persistence\GeoCacheLog\GeoCacheLogService
Loading history...
24
     */
25
    public function __construct(FieldNoteService $fieldNoteService, GeoCacheLogService $geoCacheLogService)
26
    {
27
        $this->fieldNoteService = $fieldNoteService;
28
        $this->geoCacheLogService = $geoCacheLogService;
29
    }
30
31
    /**
32
     * Creates a UploadFormData by given user id.
33
     *
34
     * @param int $userId
35
     *
36
     * @return UploadFormData
0 ignored issues
show
introduced by
UploadFormData => \Array\UploadFormData
Loading history...
37
     */
38
    public function create($userId)
39
    {
40
        $uploadFormData = new UploadFormData();
41
42
        $uploadFormData->userId = $userId;
43
        $uploadFormData->ignoreBeforeDate = $this->getLatestLogOrFieldNoteDate($userId);
44
45
        return $uploadFormData;
46
    }
47
48
    /**
49
     * Fetches the latest log or field note date.
50
     *
51
     * @param int $userId
52
     *
53
     * @return string
54
     */
55
    private function getLatestLogOrFieldNoteDate($userId)
56
    {
57
        $fieldNoteDate = $this->getLatestFieldNoteDate($userId);
58
59
        $geoCacheLogDate = $this->getLatestLogDate($userId);
60
61
        return max($fieldNoteDate, $geoCacheLogDate);
62
    }
63
64
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$userId" missing
Loading history...
65
     * @param $userId
0 ignored issues
show
Coding Style introduced by
Missing parameter name
Loading history...
introduced by
Invalid class name "$userId"
Loading history...
66
     *
67
     * @return DateTime|null
0 ignored issues
show
introduced by
DateTime => \DateTime
Loading history...
68
     */
69
    private function getLatestLogDate($userId)
0 ignored issues
show
Coding Style introduced by
Type hint "userId" missing for
Loading history...
70
    {
71
        $geoCacheLogDate = null;
72
        $geoCacheLog = $this->geoCacheLogService->getLatestUserLog($userId);
73
74
        if ($geoCacheLog) {
75
            $geoCacheLogDate = $geoCacheLog->date;
76
        }
77
78
        return $geoCacheLogDate;
79
    }
80
81
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$userId" missing
Loading history...
82
     * Fetches the latest field note date.
83
     *
84
     * @param $userId
0 ignored issues
show
Coding Style introduced by
Missing parameter name
Loading history...
introduced by
Invalid class name "$userId"
Loading history...
85
     *
86
     * @return DateTime|null
0 ignored issues
show
introduced by
DateTime => \DateTime
Loading history...
87
     */
88
    private function getLatestFieldNoteDate($userId)
0 ignored issues
show
Coding Style introduced by
Type hint "userId" missing for
Loading history...
89
    {
90
        $fieldNoteDate = null;
91
        $fieldNote = $this->fieldNoteService->getLatestUserFieldNote($userId);
92
93
        if ($fieldNote) {
94
            $fieldNoteDate = $fieldNote->date;
95
        }
96
97
        return $fieldNoteDate;
98
    }
99
}
100