| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * CategoryFactory.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +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-19 19:44:46 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +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-19 19:44:46 +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-19 19:44:46 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | use FireflyIII\Models\Category; | 
					
						
							|  |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  | use Illuminate\Database\QueryException; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class CategoryFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class CategoryFactory | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |     private User $user; | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-10-28 06:32:37 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $categoryId   = (int) $categoryId; | 
					
						
							|  |  |  |         $categoryName = (string) $categoryName; | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName)); | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |         if ('' === $categoryName && 0 === $categoryId) { | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // first by ID:
 | 
					
						
							|  |  |  |         if ($categoryId > 0) { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             /** @var null|Category $category */ | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |             $category = $this->user->categories()->find($categoryId); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:42:07 +02:00
										 |  |  |             if (null !== $category) { | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |                 return $category; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  |         if ('' !== $categoryName) { | 
					
						
							| 
									
										
										
										
											2018-03-01 20:54:50 +01:00
										 |  |  |             $category = $this->findByName($categoryName); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:42:07 +02:00
										 |  |  |             if (null !== $category) { | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |                 return $category; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 return Category::create( | 
					
						
							|  |  |  |                     [ | 
					
						
							| 
									
										
										
										
											2023-08-11 19:37:28 +02:00
										 |  |  |                         'user_id'       => $this->user->id, | 
					
						
							|  |  |  |                         'user_group_id' => $this->user->user_group_id, | 
					
						
							|  |  |  |                         'name'          => $categoryName, | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |                     ] | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |             } catch (QueryException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                 app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |                 app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |                 throw new FireflyException('400003: Could not store new category.', 0, $e); | 
					
						
							| 
									
										
										
										
											2019-10-30 20:02:21 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     public function findByName(string $name): ?Category | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-04 07:39:16 +01:00
										 |  |  |         /** @var null|Category */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $this->user->categories()->where('name', $name)->first(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |     public function setUser(User $user): void | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |