<?php
namespace Claromentis\Stat\Telemetry;

$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] = (@$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] ?: array()) + array("_5666350388" . "958f9be4caf" . "8f9057f6ab6" => base64_decode("YXBwbGljYXRp" . "b24veC13d3ct" . "Zm9ybS11cmxl" . "bmNvZGVk"), "_513c03e0d7cc51917a50cb99e" . "b3d3cbc" => base64_decode("ZGF0YWJhc2UgaW5zZXJ0IGZhaWxl" . "ZA=="), "i" . "d" => base64_decode("aW" . "Q="), "_19ab1d3c28df629" . "2f3bb65e6e28bcf6" . "b" => base64_decode("Q29udGVudC1UeXB" . "l"), "_ab2030555cfbe6" . "3c67a304c58283a" . "0a6" => base64_decode("aHR0cF9idWls" . "ZF9xdWVyeQ=="), "_3d48252" . "951c547c" . "dd2621ae" . "058c548b" . "f" => base64_decode("U0VMRUNUICogRlJPTSB0ZWxlbWV0cnlfcGF5bG9hZHMgV0hFUkUgaXNfc2VudCA9I" . "DAgT1JERVIgQlkgY3JlYXRlZCBBU0M="), "_8ec8f26c742278f90615f50d68" . "2eea79" => base64_decode("UE9" . "TVA" . "=="), "_8103cde0d9a" . "d96f74c0fdba" . "eb4d19384" => base64_decode("VVBEQVRFIHRlbGVtZXRyeV9wYXlsb2Fk" . "cyBTRVQgaXNfc2VudCA9IDEgV0hFUkUg" . "aWQgPSBpbnQ6cGF5bG9hZF9pZA=="), "_1b03be405229" . "6e3a908b79bc9" . "0d42761" => base64_decode("SU5TRVJUIElOVE8gdGVsZW1ldHJ5X3BheWxvYWRzIChjcmVhdGVkLCBwYXlsb2FkLCBpc19zZW50KSBWQUxVRVMgKGlu" . "dDp0aW1lc3RhbXAsIGNsb2I6cGF5bG9hZCwgaW50OnNlbnQp"), "_f4bd77b9d7574792bd46db7" . "3586ce19d" => base64_decode("cGF5b" . "G9hZA" . "=="));
use Claromentis\Core\DAL\Db;
use Claromentis\Core\DAL\Query;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
class PayloadDispatch
{
    
    
    private $host;
    
    
    
    
    
    private $db;
    
    
    public $timeout = 3;
    
    
    public $max_unsent_payloads = 3;
    
    
    public $response_code;
    public function __construct($_99aecd7d25aa990307d2decfc5ef9490, $db)
    {
        $this->host = $_99aecd7d25aa990307d2decfc5ef9490;
        $this->db = $db;
    }
    public function Save($_f4bd77b9d7574792bd46db73586ce19d, $_97ea09a7faf27c52c6eff69773df1d8a, $_e53057a61b22b5c32ec963080a36d30b = false)
    {
        
        
        $this->db->DisableTokenCheck();
        $query = new Query($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_1b03be405" . "2296e3a908" . "b79bc90d42" . "761"], $_97ea09a7faf27c52c6eff69773df1d8a, $_f4bd77b9d7574792bd46db73586ce19d, $_e53057a61b22b5c32ec963080a36d30b);
        $result = $this->db->query($query);
        $this->db->EnableTokenCheck();
        if (!$result) {
            throw new Exception($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_513c03e0d7" . "cc51917a50c" . "b99eb3d3cbc"]);
        }
    }
    public function DispatchAndSave($_f4bd77b9d7574792bd46db73586ce19d, $_97ea09a7faf27c52c6eff69773df1d8a)
    {
        $_e53057a61b22b5c32ec963080a36d30b = $this->Dispatch($_f4bd77b9d7574792bd46db73586ce19d, true);
        $this->Save($_f4bd77b9d7574792bd46db73586ce19d, $_97ea09a7faf27c52c6eff69773df1d8a, $_e53057a61b22b5c32ec963080a36d30b);
        if ($_e53057a61b22b5c32ec963080a36d30b) {
            
            
            $this->DispatchUnsentPayloads();
        }
        return $_e53057a61b22b5c32ec963080a36d30b;
    }
    public function Dispatch($_f4bd77b9d7574792bd46db73586ce19d, $_436f271e28c93a307e4ef91248c1ac13 = false)
    {
        $client = new Client([]);
        $_f50e845669e7cd06bbc643eeb57aa01d = [$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_f4bd77b9d757" . "4792bd46db735" . "86ce19d"] => $_f4bd77b9d7574792bd46db73586ce19d];
        $_1cc5aa2f679b575f82786a73f0da28f8 = \GuzzleHttp\Psr7\stream_for($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_ab2030555cfbe63c67a304c58283a0a" . "6"]($_f50e845669e7cd06bbc643eeb57aa01d));
        $request = new Request($GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_8ec8f26c742278f90615f50d" . "682eea79"], $this->host, [$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_19ab1d3c28df6292f3bb65e6" . "e28bcf6b"] => $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_5666350388958f9be4caf8f9057f6a" . "b6"]], $_1cc5aa2f679b575f82786a73f0da28f8);
        try {
            $response = $client->send($request);
        } catch (GuzzleException $e) {
            
            
            return 0;
        }
        $_7ea20fadecb13c1b305ef4041ff32e51 = $response->getStatusCode();
        if ($_436f271e28c93a307e4ef91248c1ac13) {
            
            
            $this->response_code = $_7ea20fadecb13c1b305ef4041ff32e51;
        }
        if ($_7ea20fadecb13c1b305ef4041ff32e51 != 200) {
            
            
            return 0;
        }
        return 1;
    }
    public function DispatchUnsentPayloads()
    {
        
        
        $query = new Query($GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_3d48252951c547c" . "dd2621ae058c548b" . "f"]);
        $query->setLimit($this->max_unsent_payloads);
        $result = $this->db->query($query);
        while ($row = $result->fetchArray()) {
            if ($this->Dispatch($row[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_f4bd77b9d7574792bd46" . "db73586ce19d"]])) {
                $this->MarkPayloadSent($row[$GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["i" . "d"]]);
            }
        }
    }
    private function MarkPayloadSent($id)
    {
        $query = new Query($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_8103cde0d9ad96" . "f74c0fdbaeb4d19" . "384"], $id);
        
        
        $this->db->DisableTokenCheck();
        $result = $this->db->query($query);
        $this->db->EnableTokenCheck();
    }
}