public function actionEdit($id = null)
 {
     $model = new CurrencyRateProvider();
     $model->loadDefaultValues();
     if ($id !== null) {
         $model = CurrencyRateProvider::findOne($id);
     }
     $post = \Yii::$app->request->post();
     if ($model->load($post) && $model->validate() && !isset($_GET['CurrencyRateProvider'])) {
         $save_result = $model->save();
         if ($save_result) {
             Yii::$app->session->setFlash('info', Yii::t('app', 'Record has been saved'));
             return $this->redirect(['edit', 'id' => $model->id]);
         } else {
             \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data'));
         }
     }
     return $this->render('form', ['model' => $model]);
 }
예제 #2
0
 public function fetchRate(CurrencyPair $currencyPair)
 {
     /** @var CurrencyRateProvider $provider */
     $provider = CurrencyRateProvider::findOne($this->provider);
     if ($provider === null) {
         throw new Exception('Provider not found');
     }
     try {
         $providerHandler = $provider->getImplementationInstance($this->httpAdapter);
         $swap = new Swap($providerHandler);
         $rate = $swap->quote($currencyPair->getBaseCurrency() . '/' . $currencyPair->getQuoteCurrency())->getValue();
         if ($this->marginMultiplier !== null && $this->marginMultiplier > 1) {
             $rate *= $this->marginMultiplier;
         }
         if ($this->fixedMargin !== null && $this->fixedMargin > 0) {
             $rate += $this->fixedMargin;
         }
     } catch (\Exception $e) {
         throw new Exception('Calculating error');
     }
     return new Rate($rate, new \DateTime());
 }