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

UploadFormDataFactory::getLatestFieldNoteDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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