1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TagFactory.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii). |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
/** @noinspection MultipleReturnStatementsInspection */ |
22
|
|
|
|
23
|
|
|
declare(strict_types=1); |
24
|
|
|
|
25
|
|
|
namespace FireflyIII\Factory; |
26
|
|
|
|
27
|
|
|
use FireflyIII\Models\Location; |
28
|
|
|
use FireflyIII\Models\Tag; |
29
|
|
|
use FireflyIII\User; |
30
|
|
|
use Illuminate\Support\Collection; |
31
|
|
|
use Log; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class TagFactory |
35
|
|
|
*/ |
36
|
|
|
class TagFactory |
37
|
|
|
{ |
38
|
|
|
/** @var Collection */ |
39
|
|
|
private $tags; |
|
|
|
|
40
|
|
|
/** @var User */ |
41
|
|
|
private $user; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @codeCoverageIgnore |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
if ('testing' === config('app.env')) { |
51
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $data |
57
|
|
|
* |
58
|
|
|
* @return Tag|null |
59
|
|
|
*/ |
60
|
|
|
public function create(array $data): ?Tag |
61
|
|
|
{ |
62
|
|
|
$zoomLevel = 0 === (int) $data['zoom_level'] ? null : (int) $data['zoom_level']; |
63
|
|
|
$latitude = 0.0 === (float) $data['latitude'] ? null : (float) $data['latitude']; |
|
|
|
|
64
|
|
|
$longitude = 0.0 === (float) $data['longitude'] ? null : (float) $data['longitude']; |
|
|
|
|
65
|
|
|
$array = [ |
66
|
|
|
'user_id' => $this->user->id, |
67
|
|
|
'tag' => trim($data['tag']), |
68
|
|
|
'tagMode' => 'nothing', |
69
|
|
|
'date' => $data['date'], |
70
|
|
|
'description' => $data['description'], |
71
|
|
|
'latitude' => null, |
72
|
|
|
'longitude' => null, |
73
|
|
|
'zoomLevel' => null, |
74
|
|
|
]; |
75
|
|
|
$tag = Tag::create($array); |
76
|
|
|
if (null !== $tag && null !== $latitude && null !== $longitude) { |
77
|
|
|
// create location object. |
78
|
|
|
$location = new Location; |
79
|
|
|
$location->latitude = $latitude; |
80
|
|
|
$location->longitude = $longitude; |
81
|
|
|
$location->zoom_level = $zoomLevel; |
82
|
|
|
$location->locatable()->associate($tag); |
83
|
|
|
$location->save(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $tag; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $tag |
91
|
|
|
* |
92
|
|
|
* @return Tag|null |
93
|
|
|
*/ |
94
|
|
|
public function findOrCreate(string $tag): ?Tag |
95
|
|
|
{ |
96
|
|
|
$tag = trim($tag); |
97
|
|
|
|
98
|
|
|
/** @var Tag $dbTag */ |
99
|
|
|
$dbTag = $this->user->tags()->where('tag', $tag)->first(); |
100
|
|
|
if (null !== $dbTag) { |
101
|
|
|
return $dbTag; |
102
|
|
|
} |
103
|
|
|
$newTag = $this->create( |
104
|
|
|
[ |
105
|
|
|
'tag' => $tag, |
106
|
|
|
'date' => null, |
107
|
|
|
'description' => null, |
108
|
|
|
'latitude' => null, |
109
|
|
|
'longitude' => null, |
110
|
|
|
'zoom_level' => null, |
111
|
|
|
] |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
return $newTag; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param User $user |
119
|
|
|
*/ |
120
|
|
|
public function setUser(User $user): void |
121
|
|
|
{ |
122
|
|
|
$this->user = $user; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|