<commit_before><?php

require_once 'vendor/autoload.php';

/**
 * Class orsAgency
 *
 * handle ors specific requests to openagency - in this case pickupagencylistrequest only
 */
class OrsAgency
{
  private $vip_core;
  private $VipCore;
  private $curl;
  private $error;
  private $err_msg;

  /**
   * orsAgency constructor.
   * @param $vip_core
   */
  public function __construct($vip_core){
    $this->vip_core = $vip_core;
    $this->VipCore = $this->initVipCore($this->vip_core);
    $this->curl = new curl();
    $this->error = FALSE;
    $this->err_msg = NULL;
  }

  /**\brief Expands one or more library object to the corresponding branch objects
   *
   * @param $agencies zero or more agencies
   * @return array of objects contaning all branch-ids in $agencies
   */
  public function expand_library($agencies) {
    $ret = $libs = $help = array();
    if ($agencies) {
      if (!is_array($agencies)) {
        $help[]->_value = $agencies->_value;
        $agencies = $help;
      }
      foreach ($agencies as $agency) {
        $libs = array_merge($libs, $this->fetch_library_list($agency->_value));
      }
      foreach (array_unique($libs) as $lib) {
        $ret[]->_value = $lib;
      }
    }
    return $ret;
  }

  /**\brief Expands one or more libraries using openagency::pickupAgencyList
   *
   * @param $agency; agency to fetch pickupAgencyList for
   * @return array of branch-ids in agency
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function fetch_library_list($agency) {
    $libs = array();

    $agency = $this->strip_agency($agency);
    $is_main_agency = FALSE;

    $start = $this->time_since();
    try {
      $aList = $this->VipCore->pickupAgencyList([$agency], [], [], [], [], [], 'aktive');
      if (isset($aList->library) && isset($aList->library[0]) && isset($aList->library[0]->pickupAgency)) {
        foreach ($aList->library[0]->pickupAgency as $lib) {
          if (isset($lib->branchType) && $lib->branchType === 'H' && $lib->branchId === $agency) {
            $is_main_agency = TRUE;
          }
        }
        foreach ($aList->library[0]->pickupAgency as $lib) {
          $libs[] = $is_main_agency ? $lib->branchId : $agency;
        }
      }
      if (empty($libs)) {
        $this->setError('cannot_find_agency');
      }
    } catch (Exception $e) {
      $this->setError('open find order service not available: ' . $e->getMessage());
      VerboseJson::log(ERROR, 'VipCore::pickupAgencyList returned ' . $e->getMessage());
    }
    VerboseJson::log(TRACE, array(
            'system' => 'ORS2',
            'query' => $agency,
            'url' => $this->vip_core['url'],
            'total_time' => $this->time_since($start))
    );
    return $libs;
  }

  /**\brief
   * Fetch branches for the agency and check against requesterAgencyId or responderAgencyId
   * @param $param request parameters as xml-object
   * @return bool FALSE if requesterAgencyId or responderAgencyId contains non-valid agency
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function check_agency_consistency(&$param) {
    $libs = $this->fetch_library_list($param->agency->_value);
    if ($this->getError()) {
      return FALSE;
    }
    if ($param->requesterAgencyId) {
      return $this->check_in_list($libs, $param->requesterAgencyId, 'requester_not_in_agency');
    }
    else if ($param->responderAgencyId) {
      return $this->check_in_list($libs, $param->responderAgencyId, 'responder_not_in_agency');
    }
    return FALSE;
  }

  /**<commit_msg>Rename to better name for config settings<commit_after><?php

require_once 'vendor/autoload.php';

/**
 * Class orsAgency
 *
 * handle ors specific requests to openagency - in this case pickupagencylistrequest only
 */
class OrsAgency
{
  private $vipcore_config;
  private $VipCore;
  private $curl;
  private $error;
  private $err_msg;

  /**
   * orsAgency constructor.
   * @param $vipcore_config
   */
  public function __construct($vipcore_config){
    $this->vipcore_config = $vipcore_config;
    $this->VipCore = $this->initVipCore($this->vipcore_config);
    $this->curl = new curl();
    $this->error = FALSE;
    $this->err_msg = NULL;
  }

  /**\brief Expands one or more library object to the corresponding branch objects
   *
   * @param $agencies zero or more agencies
   * @return array of objects contaning all branch-ids in $agencies
   */
  public function expand_library($agencies) {
    $ret = $libs = $help = array();
    if ($agencies) {
      if (!is_array($agencies)) {
        $help[]->_value = $agencies->_value;
        $agencies = $help;
      }
      foreach ($agencies as $agency) {
        $libs = array_merge($libs, $this->fetch_library_list($agency->_value));
      }
      foreach (array_unique($libs) as $lib) {
        $ret[]->_value = $lib;
      }
    }
    return $ret;
  }

  /**\brief Expands one or more libraries using openagency::pickupAgencyList
   *
   * @param $agency; agency to fetch pickupAgencyList for
   * @return array of branch-ids in agency
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function fetch_library_list($agency) {
    $libs = array();

    $agency = $this->strip_agency($agency);
    $is_main_agency = FALSE;

    $start = $this->time_since();
    try {
      $aList = $this->VipCore->pickupAgencyList([$agency], [], [], [], [], [], 'aktive');
      if (isset($aList->library) && isset($aList->library[0]) && isset($aList->library[0]->pickupAgency)) {
        foreach ($aList->library[0]->pickupAgency as $lib) {
          if (isset($lib->branchType) && $lib->branchType === 'H' && $lib->branchId === $agency) {
            $is_main_agency = TRUE;
          }
        }
        foreach ($aList->library[0]->pickupAgency as $lib) {
          $libs[] = $is_main_agency ? $lib->branchId : $agency;
        }
      }
      if (empty($libs)) {
        $this->setError('cannot_find_agency');
      }
    } catch (Exception $e) {
      $this->setError('open find order service not available: ' . $e->getMessage());
      VerboseJson::log(ERROR, 'VipCore::pickupAgencyList returned ' . $e->getMessage());
    }
    VerboseJson::log(TRACE, array(
            'system' => 'ORS2',
            'query' => $agency,
            'url' => $this->vipcore_config['url'],
            'total_time' => $this->time_since($start))
    );
    return $libs;
  }

  /**\brief
   * Fetch branches for the agency and check against requesterAgencyId or responderAgencyId
   * @param $param request parameters as xml-object
   * @return bool FALSE if requesterAgencyId or responderAgencyId contains non-valid agency
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function check_agency_consistency(&$param) {
    $libs = $this->fetch_library_list($param->agency->_value);
    if ($this->getError()) {
      return FALSE;
    }
    if ($param->requesterAgencyId) {
      return $this->check_in_list($libs, $param->requesterAgencyId, 'requester_not_in_agency');
    }
    else if ($param->responderAgencyId) {
      return $this->check_in_list($libs, $param->responderAgencyId, 'responder_not_in_agency');
    }
    return FALSE;
  }

  /**
{'fraction_non_alphanumeric': 0.15219212510471936, 'fraction_numerical': 0.0013962580284836638, 'mean_word_length': 3.008393956351427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7274547', 'n_tokens_mistral': 2399, 'n_tokens_neox': 2308, 'n_words': 502}