| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Application\Api\Field\Mutation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Application\Api\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Application\Model\Transaction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Application\Model\TransactionLine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Application\Repository\TransactionRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Ecodev\Felix\Api\Field\FieldInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use GraphQL\Type\Definition\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Mezzio\Session\SessionInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | abstract class UpdateTransaction implements FieldInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 2 |  |     public static function build(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1 |  |         yield 'updateTransaction' => fn () => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  |             'type' => Type::nonNull(_types()->getOutput(Transaction::class)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |             'description' => 'Update a transaction, and optionally replace all its transaction lines if given any', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1 |  |             'args' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |                 'id' => Type::nonNull(_types()->getId(Transaction::class)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |                 'input' => Type::nonNull(_types()->getPartialInput(Transaction::class)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 1 |  |                 'lines' => Type::listOf(Type::nonNull(_types()->getInput(TransactionLine::class))), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |             'resolve' => function ($root, array $args, SessionInterface $session): Transaction { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 /** @var Transaction $transaction */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  |                 $transaction = $args['id']->getEntity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 2 |  |                 $input = $args['input']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2 |  |                 Helper::hydrate($transaction, $input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 // Check ACL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  |                 Helper::throwIfDenied($transaction, 'update'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |                 $lines = $args['lines'] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |                 if ($lines !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                     /** @var TransactionRepository $transactionRepository */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |                     $transactionRepository = _em()->getRepository(Transaction::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |                     $transactionRepository->hydrateLinesAndFlush($transaction, $lines); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |                     _em()->refresh($transaction); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     // Update the date of each line to match the one of the transaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |                     foreach ($transaction->getTransactionLines() as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                         $line->setTransactionDate($transaction->getTransactionDate()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |                     _em()->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 | 2 |  |                 return $transaction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  |