<commit_before><?php
/**
 * Created by PhpStorm.
 * User: ianno
 * Date: 26/02/17
 * Time: 16:27
 */

/**
 * Class Sendgrid
 */
class Sendgrid
{
    private static $endPoint;
    private static $token;
    private static $senderEmail;
    private static $senderName;
    private static $reciverEmail;
    private static $emailSubject;
    private static $ccEmail;
    private static $enableDebug;
    private static $locationForDebug;
    private static $bulkEmail;

    /**
     * Used for initial configurations
     * @param $param
     */
    public function config($param)
    {
        Sendgrid::$endPoint     = 'https://api.sendgrid.com/v3/mail/send';
        Sendgrid::$token        = $param['token'];
        Sendgrid::$senderEmail  = $param['sender_email'];
        Sendgrid::$senderName   = $param['sender_name'];
        Sendgrid::$enableDebug  = $param['debug'];
    }

    /**
     * Digunakan untuk menambahkan Email Subject
     * @param $emailSubject
     */
    public function subject($emailSubject)
    {
        Sendgrid::$emailSubject = $emailSubject;
    }

    /**
     * Digunakan untuk menambahkan penerima email
     * @param String $reciverEmail
     */
    public function to($reciverEmail)
    {
        Sendgrid::$reciverEmail = $reciverEmail;
    }

    /**
     * Digunakan untuk menambahkan CC Email
     * @param $ccEmail
     */
    public function cc($ccEmail)
    {
        if(array($ccEmail))
        {

        }
        else
        {
            Sendgrid::$ccEmail;
        }
    }

    /**
     * Digunakan untuk melakukan pengiriman Email
     */
    public function sendEmail()
    {
        $ch = curl_init();

        $curl_options = array(
            CURLOPT_URL             => Sendgrid::$endPoint,
            CURLOPT_HTTPHEADER      => array(
                'Authorization: Bearer '.Sendgrid::$endPoint,
                'Content-Type: application/json'
            ),
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_POST            => 1,
            CURLOPT_POSTFIELDS      => Sendgrid::_parseJSON(),
            CURLOPT_HTTPAUTH        => 1
        );

        curl_setopt_array($ch, $curl_options);

        $result = curl_exec($ch);
        if(Sendgrid::$enableDebug == "true")
        {
            return $result;
        }
    }

    /**
     * Digunakan untuk melakukan JSON Parse terhadap seluruh data yang diterima
     */
    public function parseJSON()
    {
        $dataJson = array(
            'personalizations' => array(
                array(
                    'to' => array(
                        Sendgrid::_checkEmailDestination()
                    ),
                    'subject'   => Sendgrid::$emailSubject
                )
            ),
            'from'  => array(
                'email' => Sendgrid::$senderEmail
            ),
            'content'   => array(
                array(
                    'type'  => 'Text/plain',
                    'value' => 'Hello World!'
                )
            )
        );
        return json_encode($dataJson);
    }

    /**
     * Digunakan untuk melakukan pengecekan apakah tujuan email hanya satu atau banyak
     * Dan melakukan validasi apakah format email tujuan sudah sesuai atau belum
     *<commit_msg>Update Sendgrid.php<commit_after><?php
/**
 * Created by PhpStorm.
 * User: ianno
 * Date: 26/02/17
 * Time: 16:27
 */

/**
 * Class Sendgrid
 */
class Sendgrid
{
    private static $endPoint;
    private static $token;
    private static $senderEmail;
    private static $senderName;
    private static $reciverEmail;
    private static $emailSubject;
    private static $ccEmail;
    private static $enableDebug;
    private static $locationForDebug;
    private static $bulkEmail;
    private static $bodyEmail;
    private static $bodyTypeEmail;

    /**
     * Used for initial configurations
     * @param $param
     */
    public function config($param)
    {
        Sendgrid::$endPoint     = 'https://api.sendgrid.com/v3/mail/send';
        Sendgrid::$token        = $param['token'];
        Sendgrid::$senderEmail  = $param['sender_email'];
        Sendgrid::$senderName   = $param['sender_name'];
        Sendgrid::$enableDebug  = $param['debug'];
    }

    /**
     * Digunakan untuk menambahkan Email Subject
     * @param $emailSubject
     */
    public function subject($emailSubject)
    {
        Sendgrid::$emailSubject = $emailSubject;
    }

    /**
     * Digunakan untuk menambahkan penerima email
     * @param String $reciverEmail
     */
    public function to($reciverEmail)
    {
        Sendgrid::$reciverEmail = $reciverEmail;
    }

    /**
     * Digunakan untuk menambahkan CC Email
     * @param $ccEmail
     */
    public function cc($ccEmail)
    {
        if(array($ccEmail))
        {

        }
        else
        {
            Sendgrid::$ccEmail;
        }
    }

    /**
     * Digunakan untuk memberikan isi pada email body
     * @param string $body
     * @param string $bodyType
     */
    public function body($body, $bodyType = 'text/html')
    {
        Sendgrid::$bodyEmail        = $body;
        Sendgrid::$bodyTypeEmail    = $bodyType;
    }

    /**
     * Digunakan untuk melakukan pengiriman Email
     */
    public function send()
    {
        $ch = curl_init();

        $curl_options = array(
            CURLOPT_URL             => Sendgrid::$endPoint,
            CURLOPT_HTTPHEADER      => array(
                'Authorization: Bearer '.Sendgrid::$token,
                'Content-Type: application/json'
            ),
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_POST            => 1,
            CURLOPT_POSTFIELDS      => Sendgrid::_parseJSON(),
            CURLOPT_HTTPAUTH        => 1
        );

        curl_setopt_array($ch, $curl_options);

        $result = curl_exec($ch);
        if(Sendgrid::$enableDebug == "true")
        {
            return $result;
        }
    }

    /**
     * Digunakan untuk melakukan JSON Parse terhadap seluruh data yang diterima
     */
    private function _parseJSON()
    {
        $dataJson = array(
            'personalizations' => array(
                array(
                    'to' => array(
                        Sendgrid::_checkEmailDestination()
                    ),
                    'subject'   => Sendgrid::$emailSubject
                )
            ),
            'from'  => array(
                'email' => Sendgrid::$senderEmail
            ),
            'content'   => array(
                array(
                    'type'  => Sendgrid::$bodyTypeEmail,
                    'value' => Sendgrid::$bodyEmail
                )
            )
        );
        return json_encode($dataJson);
    }

    /**
     * Digunakan untuk melakukan pengecekan apakah tujuan email hanya satu atau banyak
     * Dan melakukan validasi apakah format email tujuan sudah sesuai atau belum
     *
{'fraction_non_alphanumeric': 0.10927536231884058, 'fraction_numerical': 0.004057971014492754, 'mean_word_length': 1.6613960663324334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3040788', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1991, 'n_words': 422}