| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright  Copyright (c) Flipbox Digital Limited | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license    https://flipboxfactory.com/software/patron/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link       https://www.flipboxfactory.com/software/patron/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace flipbox\patron\records; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Craft; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use flipbox\craft\ember\helpers\ObjectHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use flipbox\craft\ember\records\ActiveRecordTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use yii\db\ActiveQueryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property int|null $instanceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property ProviderInstance|null $instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property ActiveQueryInterface $instanceRecord | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author Flipbox Factory <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | trait InstanceMutatorTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     use ActiveRecordTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var ProviderInstance|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Set associated instanceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function setInstanceId(int $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->instanceId = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Get associated instanceId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return int|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function getInstanceId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $id = $this->getAttribute('instanceId'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if (null === $id && null !== $this->instance) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $id = $this->instance->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $this->setAttribute('instanceId', $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Associate a instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param mixed $instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function setInstance($instance = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->instance = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (null === ($instance = $this->internalResolveInstance($instance))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->instance = $this->instanceId = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             /** @var ProviderInstance $instance */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->instanceId = $instance->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->instance = $instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return ProviderInstance|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function getInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if ($this->instance === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $instance = $this->resolveInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->setInstance($instance); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             return $instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $instanceId = $this->instanceId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if ($instanceId !== null && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $instanceId !== $this->instance->id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $this->instance = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             return $this->getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return $this->instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return ProviderInstance|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     protected function resolveInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if (null !== ($model = $this->resolveInstanceFromRelation())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             return $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if (null !== ($model = $this->resolveInstanceFromId())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             return $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return ProviderInstance|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     private function resolveInstanceFromRelation() | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         if (false === $this->isRelationPopulated('instanceRecord')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         /** @var ProviderInstance $record */ | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         if (null === ($record = $this->getRelation('instanceRecord'))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         return $record; | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return ProviderInstance|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     private function resolveInstanceFromId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if (null === $this->instanceId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return ProviderInstance::findOne($this->instanceId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param $instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return ProviderInstance|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     protected function internalResolveInstance($instance = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         if ($instance instanceof ProviderInstance) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             return $instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         if (is_numeric($instance) || is_string($instance)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             /** @noinspection PhpIncompatibleReturnTypeInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             return ProviderInstance::findOne($instance); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             $object = Craft::createObject(ProviderInstance::class, [$instance]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             $object = new ProviderInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             ObjectHelper::populate( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 $object, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 $instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         /** @var ProviderInstance $object */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         return $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * Returns the associated instance record. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * @return ActiveQueryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     protected function getInstanceRecord(): ActiveQueryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         return $this->hasOne( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             ProviderInstance::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             ['id' => 'instanceId'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  |