<?php

namespace Saireh\Payroll;

use Exception;

class CurlRequest
{
    private $url;
    private $curl_session;

    public function __construct($url, $curl_session)
    {
        $this->url = $url;
        $this->curl_session = $curl_session;
    }

    public function post($curl_session_args = array())
    {
        if ($curl_session_args) {

            curl_setopt_array($this->curl_session, $curl_session_args);
        }

        $response = curl_exec($this->curl_session);
        $info = curl_getinfo($this->curl_session);
        $error = curl_error($this->curl_session);
        if ($error) {
            throw new Exception($error);
        }
        return array($response, $info);
    }

    public function get(array $curl_session_args = array())
    {
        $defaults = array(
            CURLOPT_HEADER => 0,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 4,
            CURLOPT_CUSTOMREQUEST => 'GET',
        );
        if ($curl_session_args) {
            curl_setopt_array($this->curl_session, array_replace($defaults, $curl_session_args));
        }
        $info = curl_getinfo($this->curl_session);
        $result = curl_exec($this->curl_session);
        $error = curl_error($this->curl_session);
        if ($error) {
            error_log("An exception ocurred trying to reach the service: ".var_export($error,true));
            throw new Exception($error);
        }
        return array($result, $info);
    }
}
