<commit_before><?php namespace Bedard\Webhooks\Models;

use DB;
use Model;
use Backend;
use Carbon\Carbon;
use Bedard\Webhooks\Models\Log;

/**
 * Hook Model
 */
class Hook extends Model
{

    /**
     * @var string The database table used by the model.
     */
    public $table = 'bedard_webhooks_hooks';

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [
        'name',
        'directory',
        'script',
        'http_method',
    ];

    /**
     * @var array Datetime fields
     */
    protected $dates = [
        'created_at',
        'updated_at',
        'executed_at',
    ];

    /**
     * Generate a unique token
     *
     * @return void
     */
    public function beforeCreate()
    {
        do {
            $this->token = str_random(40);
        } while (self::where('token', $this->token)->exists());
    }

    /**
     * @var array Relations
     */
    public $hasMany = [
        'logs' => [
            'Bedard\Webhooks\Models\Log',
        ],
    ];

    /**
     * Execute the script and log the output
     *
     * @return boolean
     */
    public function execute()
    {
        // Execute the hook
        if (!empty($this->directory)) {
            chdir($this->directory);
        }

        $output = `$this->script`;

        // Log the output
        Log::create([
            'hook_id' => $this->id,
            'output' => $output,
        ]);

        // Return the results
        $this->executed_at = Carbon::now();
        return $this->save();
    }

    /**
     * Left joins the logs count
     *<commit_msg>Use a more readable syntax<commit_after><?php namespace Bedard\Webhooks\Models;

use DB;
use Model;
use Backend;
use Carbon\Carbon;
use Bedard\Webhooks\Models\Log;

/**
 * Hook Model
 */
class Hook extends Model
{

    /**
     * @var string The database table used by the model.
     */
    public $table = 'bedard_webhooks_hooks';

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [
        'name',
        'directory',
        'script',
        'http_method',
    ];

    /**
     * @var array Datetime fields
     */
    protected $dates = [
        'created_at',
        'updated_at',
        'executed_at',
    ];

    /**
     * Generate a unique token
     *
     * @return void
     */
    public function beforeCreate()
    {
        do {
            $this->token = str_random(40);
        } while (self::where('token', $this->token)->exists());
    }

    /**
     * @var array Relations
     */
    public $hasMany = [
        'logs' => [
            'Bedard\Webhooks\Models\Log',
        ],
    ];

    /**
     * Execute the script and log the output
     *
     * @return boolean
     */
    public function execute()
    {
        // Execute the hook
        if (!empty($this->directory)) {
            chdir($this->directory);
        }

        $output = shell_exec($this->script);

        // Log the output
        Log::create([
            'hook_id' => $this->id,
            'output' => $output,
        ]);

        // Return the results
        $this->executed_at = Carbon::now();
        return $this->save();
    }

    /**
     * Left joins the logs count
     *
{'fraction_non_alphanumeric': 0.1452712718648088, 'fraction_numerical': 0.0011858879335902757, 'mean_word_length': 1.5696877380045697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, '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': '6134122', 'n_tokens_mistral': 1037, 'n_tokens_neox': 1013, 'n_words': 240}