<commit_before><?php

/**
 * @author troussos
 */

namespace elevate;

use DOMDocument;
use elevate\Exceptions\HVCommunicatorAuthenticationExpiredException;
use elevate\util\HVClientHelper;
use JMS\Serializer\SerializerBuilder;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use elevate\util\InfoHelper;
use SimpleXMLElement;

use elevate\Interfaces\HVClientInterface;
use elevate\Interfaces\HVCommunicatorInterface;


/**
 * Class HVClient
 * @package elevate
 */
class HVClient implements HVClientInterface, LoggerAwareInterface
{

    /**
     * @var string HV Application ID
     */
    private $appId;
    /**
     * @var array Array of extra HV configuration parameters
     */
    private $config;
    /**
     * @var HVCommunicator|null Instance of the HV Raw Connector
     */
    private $connector = NULL;
    /**
     * @var string Person Id for the HV Request being made
     */
    private $personId;
    /**
     * @var NullLogger|null Logger for HV Client
     */
    private $logger = NULL;
    /**
     * @var string URL to make HV calls against
     */
    private $healthVaultPlatform = 'https://platform.healthvault-ppe.com/platform/wildcat.ashx';
    /**
     * @var string URL to Authenticate the HV connection against
     */
    private $healthVaultAuthInstance = 'https://account.healthvault-ppe.com/redirect.aspx';
    /**
     * @var null|string HV Certificate Thumb Print
     */
    private $thumbPrint = NULL;
    /**
     * @var null|string HV Private Key for the Application
     */
    private $privateKey = NULL;
    /**
     * @var \JMS\Serializer\Serializer|null Serializer for working with the xml from HV
     */
    private $serializer = NULL;
    /**
     * @var null|string Authentication Token from Health Vault
     */
    private $authToken = NULL;

    /**
     * @var string Used to sign the HV calls.
     */
    private $digest = NULL;

    /**
     * Set the member variables to the passed HV credentials. Setup a Logger and create the serializer.
     *
     * @param $thumbPrint
     * @param $privateKey
     * @param $appId
     * @param $personId
     * @param $recordId
     */
    public function __construct(
        $thumbPrint,
        $privateKey,
        $appId,
        $personId,
        $recordId
    )
    {
        $this->thumbPrint = $thumbPrint;
        $this->privateKey = $privateKey;
        $this->appId      = $appId;
        $this->personId   = $personId;
        $this->recordId   = $recordId;

        $builder          = new SerializerBuilder();
        $this->serializer = $builder->build();

        $this->logger = new NullLogger();
        return $this;
    }

    /**
     * @return $this
     */
    public function disconnect()
    {
        $this->config['healthVault'] = NULL;
        $this->connector             = NULL;
        $this->connection            = NULL;
        $this->setAuthToken(NULL);
        return $this;
    }

    /**
     * @param      $redirectUrl
     * @param null $target
     * @param null $additionalTargetQSParams
     *
     * @return string
     */
    public function getAuthenticationURL(
        $redirectUrl,
        $target = NULL,
        $additionalTargetQSParams = NULL
    )
    {
        return HVCommunicator::getAuthenticationURL(
            $this->appId,
            $redirectUrl,
            $this->config,
            $this->healthVaultAuthInstance,
            $target,
            $additionalTargetQSParams
        );
    }

    public function getPersonInfo()
    {
        $method  = 'GetPersonInfo';
        $version = 1;
        $response = $this->callHealthVault(NULL, $method, $version);
        return HVClientHelper::HVPersonFromXML($response);
    }

    public function callHealthVault($info, $method, $version)
    {
        $xml = HVClientHelper::HVInfoAsXML($info);
        $xml = str_replace('<personal-image><![CDATA[]]></personal-image>','<personal-image />',$xml);
        $xml = str_replace('<appointment_attended><![CDATA[]]></appointment_attended>', '<appointment_attended><![CDATA[0]]></appointment_attended>', $xml);
        $newXML    = "";
        //check if this is a group request
        if(!empty($info)){<commit_msg>Fix to HVCommunicator arguments<commit_after><?php

/**
 * @author troussos
 */

namespace elevate;

use DOMDocument;
use elevate\Exceptions\HVCommunicatorAuthenticationExpiredException;
use elevate\util\HVClientHelper;
use JMS\Serializer\SerializerBuilder;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use elevate\util\InfoHelper;
use SimpleXMLElement;

use elevate\Interfaces\HVClientInterface;
use elevate\Interfaces\HVCommunicatorInterface;


/**
 * Class HVClient
 * @package elevate
 */
class HVClient implements HVClientInterface, LoggerAwareInterface
{

    /**
     * @var string HV Application ID
     */
    private $appId;
    /**
     * @var array Array of extra HV configuration parameters
     */
    private $config;
    /**
     * @var HVCommunicator|null Instance of the HV Raw Connector
     */
    private $connector = NULL;
    /**
     * @var string Person Id for the HV Request being made
     */
    private $personId;
    /**
     * @var NullLogger|null Logger for HV Client
     */
    private $logger = NULL;
    /**
     * @var string URL to make HV calls against
     */
    private $healthVaultPlatform = 'https://platform.healthvault-ppe.com/platform/wildcat.ashx';
    /**
     * @var string URL to Authenticate the HV connection against
     */
    private $healthVaultAuthInstance = 'https://account.healthvault-ppe.com/redirect.aspx';
    /**
     * @var null|string HV Certificate Thumb Print
     */
    private $thumbPrint = NULL;
    /**
     * @var null|string HV Private Key for the Application
     */
    private $privateKey = NULL;
    /**
     * @var \JMS\Serializer\Serializer|null Serializer for working with the xml from HV
     */
    private $serializer = NULL;
    /**
     * @var null|string Authentication Token from Health Vault
     */
    private $authToken = NULL;

    /**
     * @var string Used to sign the HV calls.
     */
    private $digest = NULL;

    /**
     * Set the member variables to the passed HV credentials. Setup a Logger and create the serializer.
     *
     * @param $thumbPrint
     * @param $privateKey
     * @param $appId
     * @param $personId
     * @param $recordId
     */
    public function __construct(
        $thumbPrint,
        $privateKey,
        $appId,
        $personId,
        $recordId
    )
    {
        $this->thumbPrint = $thumbPrint;
        $this->privateKey = $privateKey;
        $this->appId      = $appId;
        $this->personId   = $personId;
        $this->recordId   = $recordId;

        $builder          = new SerializerBuilder();
        $this->serializer = $builder->build();

        $this->logger = new NullLogger();
        return $this;
    }

    /**
     * @return $this
     */
    public function disconnect()
    {
        $this->config['healthVault'] = NULL;
        $this->connector             = NULL;
        $this->connection            = NULL;
        $this->setAuthToken(NULL);
        return $this;
    }

    /**
     * @param      $redirectUrl
     * @param null $target
     * @param null $additionalTargetQSParams
     *
     * @return string
     */
    public function getAuthenticationURL(
        $redirectUrl,
        $target = NULL,
        $additionalTargetQSParams = NULL
    )
    {
        return HVCommunicator::getAuthenticationURL(
            $this->appId,
            $redirectUrl,
            $this->config,
            $additionalTargetQSParams,
            $this->healthVaultAuthInstance,
            $target
        );
    }

    public function getPersonInfo()
    {
        $method  = 'GetPersonInfo';
        $version = 1;
        $response = $this->callHealthVault(NULL, $method, $version);
        return HVClientHelper::HVPersonFromXML($response);
    }

    public function callHealthVault($info, $method, $version)
    {
        $xml = HVClientHelper::HVInfoAsXML($info);
        $xml = str_replace('<personal-image><![CDATA[]]></personal-image>','<personal-image />',$xml);
        $xml = str_replace('<appointment_attended><![CDATA[]]></appointment_attended>', '<appointment_attended><![CDATA[0]]></appointment_attended>', $xml);
        $newXML    = "";
        //check if this is a group request
        if(!empty($info)){
{'fraction_non_alphanumeric': 0.11962174940898346, 'fraction_numerical': 0.00047281323877068556, 'mean_word_length': 2.299921996879875, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 55, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5969228', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2405, 'n_words': 586}