| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Copyright (C) eZ Systems AS. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license For full copyright and license information view LICENSE file distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace eZ\Publish\Core\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use eZ\Publish\API\Repository\UserPreferenceService as UserPreferenceServiceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use eZ\Publish\API\Repository\Values\UserPreference\UserPreference as APIUserPreference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use eZ\Publish\API\Repository\Values\UserPreference\UserPreferenceList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use eZ\Publish\SPI\Persistence\UserPreference\UserPreferenceSetStruct; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use eZ\Publish\SPI\Persistence\UserPreference\Handler as UserPreferenceHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use eZ\Publish\SPI\Persistence\UserPreference\UserPreference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use eZ\Publish\API\Repository\Repository as RepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class UserPreferenceService implements UserPreferenceServiceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var \eZ\Publish\API\Repository\Repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var \eZ\Publish\SPI\Persistence\UserPreference\Handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $userPreferenceHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param \eZ\Publish\API\Repository\Repository $repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param \eZ\Publish\SPI\Persistence\UserPreference\Handler $userPreferenceHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function __construct(RepositoryInterface $repository, UserPreferenceHandler $userPreferenceHandler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->repository = $repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->userPreferenceHandler = $userPreferenceHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 46 |  | View Code Duplication |     public function loadUserPreferences(int $offset = 0, int $limit = 25): UserPreferenceList | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $currentUserId = $this->getCurrentUserId(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $list = new UserPreferenceList(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $list->totalCount = $this->userPreferenceHandler->countUserPreferences($currentUserId); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         if ($list->totalCount > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $list->items = array_map(function (UserPreference $spiUserPreference) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 return $this->buildDomainObject($spiUserPreference); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             }, $this->userPreferenceHandler->loadUserPreferences($currentUserId, $offset, $limit)); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         return $list; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function setUserPreference(array $userPreferenceSetStructs): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $spiSetStructs = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         foreach ($userPreferenceSetStructs as $key => $userPreferenceSetStruct) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $spiSetStruct = new UserPreferenceSetStruct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $spiSetStruct->userId = $this->getCurrentUserId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             if (empty($userPreferenceSetStruct->name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 throw new InvalidArgumentException('name', $userPreferenceSetStruct->name . ' at index ' . $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $spiSetStruct->name = $userPreferenceSetStruct->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $value = (string)$userPreferenceSetStruct->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } catch (\Exception $exception) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 throw new InvalidArgumentException('value', 'Can not convert value to string at index ' . $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $spiSetStruct->value = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $spiSetStructs[] = $spiSetStruct; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->repository->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             foreach ($spiSetStructs as $spiSetStruct) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $this->userPreferenceHandler->setUserPreference($spiSetStruct); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $this->repository->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $this->repository->rollback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function getUserPreference(string $userPreferenceName): APIUserPreference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $currentUserId = $this->getCurrentUserId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $userPreference = $this->userPreferenceHandler->getUserPreferenceByUserIdAndName( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $currentUserId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $userPreferenceName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $this->buildDomainObject($userPreference); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function getUserPreferenceCount(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return $this->userPreferenceHandler->countUserPreferences( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $this->getCurrentUserId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Builds UserPreference domain object from ValueObject returned by Persistence API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param \eZ\Publish\SPI\Persistence\UserPreference\UserPreference $spiUserPreference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @return \eZ\Publish\API\Repository\Values\UserPreference\UserPreference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     protected function buildDomainObject(UserPreference $spiUserPreference): APIUserPreference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return new APIUserPreference([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             'name' => $spiUserPreference->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             'value' => $spiUserPreference->value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     private function getCurrentUserId(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return $this->repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             ->getPermissionResolver() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             ->getCurrentUserReference() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             ->getUserId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  | 
            
                        
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.