| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright  Copyright (c) Flipbox Digital Limited | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link       https://github.com/flipboxfactory/craft-ember/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace flipbox\craft\ember\objects; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Craft; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use craft\models\Site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * This trait accepts both an Site or and Site Id and ensures that the both | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * the Site and the Id are in sync; If one changes (and does not match the other) it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * resolves (removes / updates) the other. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * In addition, this trait is primarily useful when a new Site is set and saved; the Site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Id can be retrieved without needing to explicitly set the newly created Id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @property Site|null $site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Flipbox Factory <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @since 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | trait SiteMutatorTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var Site|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Internally set the Site Id.  This can be overridden. A record for example | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * should use `setAttribute`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param int|null $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     abstract protected function internalSetSiteId(int $id = null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Internally get the Site Id.  This can be overridden.  A record for example | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * should use `getAttribute`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return int|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     abstract protected function internalGetSiteId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function isSiteSet(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return null !== $this->site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Set associated siteId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function setSiteId(int $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->internalSetSiteId($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (null !== $this->site && $id != $this->site->id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->site = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Get associated siteId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return int|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function getSiteId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (null === $this->internalGetSiteId() && null !== $this->site) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->setSiteId($this->site->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $this->internalGetSiteId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Associate a site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param mixed $site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function setSite($site = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->site = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->internalSetSiteId(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (null !== ($site = $this->verifySite($site))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $this->site = $site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->internalSetSiteId($site->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return Site|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function getSite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         if ($this->site === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $site = $this->resolveSite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $this->setSite($site); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return $site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $siteId = $this->internalGetSiteId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if ($siteId !== null && $siteId != $this->site->id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $this->site = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             return $this->getSite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return $this->site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @return Site|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     protected function resolveSite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         if ($site = $this->resolveSiteFromId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             return $site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return Site|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     private function resolveSiteFromId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         if (null === ($siteId = $this->internalGetSiteId())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         return Craft::$app->getSites()->getSiteById($siteId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * Attempt to verify that the passed 'site' is a valid element.  A primary key or query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * can be passed to lookup an site. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @param mixed $site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @return Site|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 159 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |     protected function verifySite($site = null) | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |         if (null === $site) { | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |             return null; | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |         if ($site instanceof Site) { | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |             return $site; | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |         if (is_numeric($site)) { | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |             return Craft::$app->getSites()->getSiteById($site); | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |         if (is_string($site)) { | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |             return Craft::$app->getSites()->getSiteByHandle($site); | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |         return null; | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 180 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |  |