| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | * NOTICE OF LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Part of the Rinvex Repository Package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * This source file is subject to The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | * that is bundled with this package in the LICENSE file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Package: Rinvex Repository Package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | * License: The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | * Link:    https://rinvex.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace Rinvex\Repository\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | trait Transactionable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Create a new entity with the given attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param array $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function create(array $attributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         // Start transaction! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $result = parent::create($attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             // Rollback if something went wrong | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $this->rollback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         // Commit the queries! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Update an entity with the given attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param mixed $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param array $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function update($id, array $attributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         // Start transaction! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $result = parent::update($id, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             // Rollback if something went wrong | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $this->rollback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // Commit the queries! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Delete an entity with the given id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param mixed $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function delete($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // Start transaction! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $result = parent::delete($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             // Rollback if something went wrong | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $this->rollback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         // Commit the queries! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Start a new database transaction. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function beginTransaction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->getContainer('db')->beginTransaction(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Commit the active database transaction. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     public function commit() | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         $this->getContainer('db')->commit(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Rollback the active database transaction. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function rollBack() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->getContainer('db')->rollback(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  | 
            
                        
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.