| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * TransactionJournalMetaFactory.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01: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-02-18 16:35:26 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01: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-02-18 16:35:26 +01: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-02-18 16:35:26 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  | /** @noinspection MultipleReturnStatementsInspection */ | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  | use FireflyIII\Models\TransactionJournalMeta; | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class TransactionJournalMetaFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class TransactionJournalMetaFactory | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-21 08:51:30 +01:00
										 |  |  |      * @return TransactionJournalMeta|null | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-21 08:51:30 +01:00
										 |  |  |     public function updateOrCreate(array $data): ?TransactionJournalMeta | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:07 +02:00
										 |  |  |         //Log::debug('In updateOrCreate()');
 | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |         $value = $data['data']; | 
					
						
							| 
									
										
										
										
											2018-02-21 08:51:30 +01:00
										 |  |  |         /** @var TransactionJournalMeta $entry */ | 
					
						
							|  |  |  |         $entry = $data['journal']->transactionJournalMeta()->where('name', $data['name'])->first(); | 
					
						
							| 
									
										
										
										
											2018-03-30 22:40:20 +02:00
										 |  |  |         if (null === $value && null !== $entry) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:07 +02:00
										 |  |  |             //Log::debug('Value is empty, delete meta value.');
 | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 $entry->delete(); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |             } catch (Exception $e) { // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |                 Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-02-21 08:51:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |         if ($data['data'] instanceof Carbon) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:07 +02:00
										 |  |  |             //Log::debug('Is a carbon object.');
 | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |             $value = $data['data']->toW3cString(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if ('' === (string)$value) { | 
					
						
							| 
									
										
										
										
											2021-04-24 21:18:15 +02:00
										 |  |  |             // Log::debug('Is an empty string.');
 | 
					
						
							| 
									
										
										
										
											2018-03-10 20:25:11 +01:00
										 |  |  |             // don't store blank strings.
 | 
					
						
							| 
									
										
										
										
											2018-03-30 22:40:20 +02:00
										 |  |  |             if (null !== $entry) { | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |                 Log::debug('Will not store empty strings, delete meta value'); | 
					
						
							| 
									
										
										
										
											2018-03-11 08:22:20 +01:00
										 |  |  |                 try { | 
					
						
							|  |  |  |                     $entry->delete(); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |                 } catch (Exception $e) { // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |                     Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2018-03-11 08:22:20 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-03-10 20:25:11 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null === $entry) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:07 +02:00
										 |  |  |             //Log::debug('Will create new object.');
 | 
					
						
							| 
									
										
										
										
											2018-06-02 18:19:35 +02:00
										 |  |  |             Log::debug(sprintf('Going to create new meta-data entry to store "%s".', $data['name'])); | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |             $entry = new TransactionJournalMeta(); | 
					
						
							|  |  |  |             $entry->transactionJournal()->associate($data['journal']); | 
					
						
							|  |  |  |             $entry->name = $data['name']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |         Log::debug('Will update value and return.'); | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |         $entry->data = $value; | 
					
						
							|  |  |  |         $entry->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $entry; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |