<commit_before><?php
/**
 * Created by PhpStorm.
 * User: jeremy.dubois
 * Date: 23/10/14
 * Time: 10:52
 */

namespace Core\Service;


use Zend\ServiceManager\ServiceLocatorAwareInterface;


/**
 * Email Helper
 * Class Email
 * @package Core\Service
 */
class Notifications extends CoreService implements ServiceLocatorAwareInterface
{
    private $slack_url;
    protected function getSlackURL()
    {
        if(!isset($this->slack_url))
        {
            $config = $this->sm->get("AppConfig")->getConfiguration();
            if(isset($config["slack"]["token"]) && isset($config["slack"]["name"]))
            {
                $this->slack_url = "https://".$config["slack"]["name"].".slack.com/services/hooks/slackbot?token=".$config["slack"]["token"];
            }
        }
        return $this->slack_url;
    }

    public function sendError($info)
    {
        if($info["message"] == "You are not allowed to be on this page")
        {
            //ignore
            return;
        }
        return;
        $channel = "test_yb";

        $message = ":coffee:\t ".(isset($info["user"])?$info["user"]->first_name." ".$info["user"]->last_name." ":"")." ".($info["id_user"]!=0?'('.$info["id_user"].')':'(no id)');
        $message .= "\n".$info["message"];

        $message .= "\n".$info["url"];
        $file = $info["file"];

        $index = mb_strpos($file, "module/");
        if($index!==False)
        {
            $file = mb_substr($info["file"], $index+7);
        }
        $message .= "\n".$file.":".$info["line"]."\n";
        return $this->sendNotification($channel, $message);
    }

    public function sendNotification($channel, $message)
    {

            $url = $this->getSlackURL();
            if(!isset($url))
            {
                //no slack configuration
                return;
            }
            //local redirection
            $config = $this->sm->get("AppConfig")->getConfiguration(); 

            if(isset($config["local_notifications"]["slack_channel"]))
            {
                $channel = $config["local_notifications"]["slack_channel"];
            }else
            {
                if(!$this->sm->get("AppConfig")->isProduction())
                {
                    $channel = "test_yb";
                }   
            }

            $url .= "&channel=%23".$channel;
            $data  = $message;
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($ch);
            curl_close($ch);
            return $result;
    }
}<commit_msg>new system for slack notification<commit_after><?php
/**
 * Created by PhpStorm.
 * User: jeremy.dubois
 * Date: 23/10/14
 * Time: 10:52
 */

namespace Core\Service;


use Zend\ServiceManager\ServiceLocatorAwareInterface;

/**
 * Email Helper
 * Class Email
 * @package Core\Service
 */
class Notifications extends CoreService implements ServiceLocatorAwareInterface
{
    private $slack_url;
    private $client;

    public function init()
    {
        $config = $this->getServiceLocator()->get("AppConfig")->get('slack');

        $this->slack_url = $config['url'];
    }

    public function sendError($info)
    {
        // if($info["message"] == "You are not allowed to be on this page")
        // {
        //     //ignore
        //     return;
        // }
        // return;
        // $channel = "test_yb";

        // $message = ":coffee:\t ".(isset($info["user"])?$info["user"]->first_name." ".$info["user"]->last_name." ":"")." ".($info["id_user"]!=0?'('.$info["id_user"].')':'(no id)');
        // $message .= "\n".$info["message"];

        // $message .= "\n".$info["url"];
        // $file = $info["file"];

        // $index = mb_strpos($file, "module/");
        // if($index!==False)
        // {
        //     $file = mb_substr($info["file"], $index+7);
        // }
        // $message .= "\n".$file.":".$info["line"]."\n";
        // return $this->sendNotification($channel, $message);
    }

    public function sendNotification($channel, $message, $attachments = [], $bot_name = null, $icon = null)
    {
        $data = array(
            'channel'     => $channel,
            'username'    => $bot_name,
            'text'        => $message,
            'icon_emoji'  => $icon,
            'attachments' => $attachments
        );

        $data_string = json_encode($data);

        $ch = curl_init( $this->slack_url );

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }
}
{'fraction_non_alphanumeric': 0.18533360048086556, 'fraction_numerical': 0.005209376878381086, 'mean_word_length': 2.0364963503649633, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 32, 'https://': 1, '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': '98255', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1532, 'n_words': 254}