| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * AttachmentFactory.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  | use FireflyIII\Models\Attachment; | 
					
						
							|  |  |  | use FireflyIII\Models\Note; | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AttachmentFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AttachmentFactory | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-22 12:24:01 +02:00
										 |  |  |     private User $user; | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function create(array $data): ?Attachment | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  |         // append if necessary.
 | 
					
						
							| 
									
										
										
										
											2024-06-15 16:16:40 +02:00
										 |  |  |         $model      = !str_contains($data['attachable_type'], 'FireflyIII') ? sprintf('FireflyIII\Models\%s', $data['attachable_type']) | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             : $data['attachable_type']; | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-16 13:15:32 +02:00
										 |  |  |         // get journal instead of transaction.
 | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  |         if (Transaction::class === $model) { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             /** @var null|Transaction $transaction */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $transaction           = $this->user->transactions()->find((int)$data['attachable_id']); | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  |             if (null === $transaction) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |                 throw new FireflyException('Unexpectedly could not find transaction'); | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |             $data['attachable_id'] = $transaction->transaction_journal_id; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             $model                 = TransactionJournal::class; | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |         // create attachment:
 | 
					
						
							|  |  |  |         $attachment = Attachment::create( | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 'user_id'         => $this->user->id, | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |                 'attachable_id'   => $data['attachable_id'], | 
					
						
							| 
									
										
										
										
											2018-12-21 10:11:18 +01:00
										 |  |  |                 'attachable_type' => $model, | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |                 'md5'             => '', | 
					
						
							|  |  |  |                 'filename'        => $data['filename'], | 
					
						
							|  |  |  |                 'title'           => '' === $data['title'] ? null : $data['title'], | 
					
						
							|  |  |  |                 'description'     => null, | 
					
						
							|  |  |  |                 'mime'            => '', | 
					
						
							|  |  |  |                 'size'            => 0, | 
					
						
							|  |  |  |                 'uploaded'        => 0, | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |         $notes      = (string)($data['notes'] ?? ''); | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |         if ('' !== $notes) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $note       = new Note(); | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |             $note->noteable()->associate($attachment); | 
					
						
							|  |  |  |             $note->text = $notes; | 
					
						
							|  |  |  |             $note->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $attachment; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setUser(User $user): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |