| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Omatech\Mage\Core\Domains\Permissions\Features; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Omatech\Mage\Core\Domains\Permissions\Exceptions\PermissionAlreadyExistsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Omatech\Mage\Core\Domains\Permissions\Exceptions\PermissionDoesNotExistsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Omatech\Mage\Core\Domains\Permissions\Exceptions\PermissionNameExistsMustBeUniqueException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Omatech\Mage\Core\Domains\Permissions\Jobs\CreatePermission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Omatech\Mage\Core\Domains\Permissions\Jobs\ExistsPermission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Omatech\Mage\Core\Domains\Permissions\Jobs\UniquePermission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Omatech\Mage\Core\Domains\Permissions\Jobs\UpdatePermission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Omatech\Mage\Core\Domains\Permissions\Permission; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class UpdateOrCreatePermission | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     private $exists; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     private $create; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     private $update; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     private $unique; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * UpdateOrCreatePermission constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 21 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 21 |  |         $this->exists = new ExistsPermission(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 21 |  |         $this->create = new CreatePermission(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 21 |  |         $this->update = new UpdatePermission(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 21 |  |         $this->unique = new UniquePermission(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 | 21 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param Permission $permission | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @throws PermissionAlreadyExistsException | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @throws PermissionDoesNotExistsException | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @throws PermissionNameExistsMustBeUniqueException | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 21 |  |     public function make(Permission $permission): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 21 |  |         if (null !== $permission->getId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |             return $this->update($permission); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 21 |  |         return $this->create($permission); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param Permission $permission | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @throws PermissionAlreadyExistsException | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 21 |  |     private function create(Permission $permission): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 21 |  |         $exists = $this->exists->make($permission); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 21 |  |         if (true === $exists) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |             throw new PermissionAlreadyExistsException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 21 |  |         return $this->create->make($permission); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param Permission $permission | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @throws PermissionDoesNotExistsException | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @throws PermissionNameExistsMustBeUniqueException | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |     private function update(Permission $permission): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |         $exists = $this->unique->make($permission); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |         if (true === $exists) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             throw new PermissionNameExistsMustBeUniqueException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         $exists = $this->exists->make($permission); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |         if (false === $exists) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |             throw new PermissionDoesNotExistsException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         return $this->update->make($permission); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  |