/** * Magic getter. * Gets the values of the properties that require initialization. * * @param string $name Property name * * @return mixed Requested property */ public function __get($name) { switch ($name) { case 'properties': if (empty($this->_properties)) { $this->_properties = new SettingsCollection('Scalr\\Model\\Entity\\CloudCredentialsProperty', [['cloudCredentialsId' => &$this->id]], ['cloudCredentialsId' => &$this->id]); } return $this->_properties; case 'environments': if (!$this->envsLoaded) { $this->_envBinds = []; /* @var $envCloudCreds EnvironmentCloudCredentials */ foreach (EnvironmentCloudCredentials::findByCloudCredentialsId($this->id) as $envCloudCreds) { $this->_envBinds[$envCloudCreds->envId] = $envCloudCreds; } $this->envsLoaded = true; } return $this->_envBinds; default: return parent::__get($name); } }
/** * Binds current cloud credential to environment * * @param Scalr_Environment $environment Environment which linked credentials * * @return CloudCredentials * * @throws Exception * @throws \Scalr\Exception\ModelException */ public function bindToEnvironment(Scalr_Environment $environment) { if (empty($this->id)) { throw new UnderflowException("CloudCredentials entity must be saved before!"); } /* @var $previousCloudCreds CloudCredentials */ $previousCloudCreds = $environment->cloudCredentials($this->cloud); if (!empty($previousCloudCreds->id) && $previousCloudCreds->id == $this->id) { return $this; } $envCloudCreds = EnvironmentCloudCredentials::findPk($environment->id, $this->cloud); if (empty($envCloudCreds)) { $envCloudCreds = new EnvironmentCloudCredentials(); $envCloudCreds->envId = $environment->id; $envCloudCreds->cloud = $this->cloud; } $db = $this->db(); try { $db->BeginTrans(); $envCloudCreds->cloudCredentialsId = $this->id; $envCloudCreds->save(); //NOTE: temporary if (!empty($previousCloudCreds) && empty(EnvironmentCloudCredentials::findByCloudCredentialsId($previousCloudCreds->id))) { $previousCloudCreds->delete(); } $db->CommitTrans(); } catch (Exception $e) { $db->RollbackTrans(); throw $e; } $this->_envBinds[$envCloudCreds->envId] = $envCloudCreds; return $this; }