<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace KtoPruef;

use Logger;

/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */
class WebCheck implements KtoPruefInterface
{
  private $webservice_url = WEBSERVICE_URL;
  private $isCurlInstalled = true;
  private $cache = array();

  public function __construct()
  {
    if (!function_exists('curl_init')) {
      Logger::logg('Konnte Webservice nicht kontaktieren, da Curl nicht installiert ist.', 1890);
      $this->isCurlInstalled = false;
    }
  }

  public function checkKto($blz, $kto)
  {
    $result = $this->doRequest('dta/check', array('blz' => $blz, 'kto' => $kto), 'POST');
    if ($result && $result['result'] == 'true') {
      return true;
    } else {
      return $result['reason'] == 'blz_is_incorrect';
    }
  }

  public function checkBLZ($blz)
  {
    $result = $this->doRequest('dta/info', array('blz' => $blz), 'POST');
    if ($result && $result['result'] == 'true') {
      return true;
    } else {
      return false;
    }
  }

  public function getBank4Blz($blz)
  {
    $result = $this->doRequest('dta/info', array('blz' => $blz), 'POST');
    if ($result && $result['result'] == 'true') {
      return $result['info'];
    } else {
      return false;
    }
  }

  public function checkIBAN($iban)
  {
    $result = $this->doRequest('sepa/check', array('iban' => $iban), 'POST');
    if ($result && $result['result'] == 'true') {
      return true;
    } else {
      return false;
    }
  }

  public function checkBic($bic)
  {
    $result = $this->doRequest('sepa/info', array('bic' => $bic), 'POST');
    if ($result && $result['result'] == 'true') {
      return true;
    } else {
      return false;
    }
  }

  public function getBank4Bic($bic)
  {
    $result = $this->doRequest('sepa/info', array('bic' => $bic), 'POST');
    if ($result && $result['result'] == 'true') {
      return $result['info'];
    } else {
      return false;
    }
  }

  private function doRequest($route, $params = array(), $type = 'GET')
  {
    
    if (!$this->isCurlInstalled) {
      return false;
    }

    
    if (!in_array($type, array('GET', 'POST'))) {
      $type = 'GET';
    }

    
    $hash = md5($route . serialize($params) . $type);

    
    if (!key_exists($hash, $this->cache)) {
      $this->cache[$hash] = $this->doCurlRequest($route, $params, $type);
    }

    if ($this->cache[$hash] && is_array($this->cache[$hash]) && $this->cache[$hash]['status'] == 'OK') {
      return $this->cache[$hash];
    } elseif ($this->cache[$hash]['status'] == 'error') {
      $ret = $this->cache[$hash];
      Logger::logg('Fehler beim Aufruf des Webservices: "' . serialize($ret) . '"', 1891);
    }

    return false;
  }

  private function doCurlRequest($route, $params, $type)
  {
    $url = $this->webservice_url . $route;
    $curl = \RequestOperations::getStandardCurl($url, $params, $type);
    $response = curl_exec($curl);

    $ret = false;
    if ($response) {
      $jsonDecodenResponse = json_decode($response, true);
      
      
      
      if ($jsonDecodenResponse) {
        $encoder = new \EncodeCharsetFromTo(UTF_8, \CodingOperations::getInternalEncoding());
        $ret = $encoder->encodeRecursive($jsonDecodenResponse);
      }
    } else {
      Logger::logg('KtoPruef-Curl Fehler: ' . curl_error($curl), 1892);
    }

    return $ret;
  }
}