<commit_before><?php
namespace Notifications\Model\Table;

use Cake\Core\Configure;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Cake\Validation\Validator;
use Notifications\Model\Entity\Notification;
use Notifications\Transport\Transport;

/**
 * NotificationQueues Model
 */
class NotificationQueueTable extends Table {

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
	public function initialize(array $config) {
		$this->table('notification_queue');
		$this->displayField('id');
		$this->primaryKey('id');
		$this->addBehavior('Timestamp');

		$this->entityClass('Notifications\Model\Entity\Notification');

		$this->belongsTo('RecipientUsers', [
			'foreignKey' => 'recipient_user_id',
			'className' => 'Users',
		]);

		$this->schema()->columnType('config', 'json');
	}

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator The validator to use when validating the entity.
 * @return \Cake\Validation\Validator
 */
	public function validationDefault(Validator $validator) {
		$validator
			->add('id', 'valid', ['rule' => 'uuid'])
			->allowEmpty('id', 'create')
			->requirePresence('locale', 'create')
			->notEmpty('locale')
			->add('recipient_user_id', 'valid', ['rule' => 'uuid'])
			->requirePresence('recipient_user_id', 'create')
			->notEmpty('recipient_user_id')
			->allowEmpty('notification_identifier')
			->requirePresence('config', 'create')
			->notEmpty('config')
			->requirePresence('transport', 'create')
			->notEmpty('transport')
			->add('locked', 'valid', ['rule' => 'boolean'])
			->allowEmpty('locked')
			->notEmpty('send_tries')
			->allowEmpty('debug');

		return $validator;
	}

/**
 * Creates a Notification entity
 *
 * @param string $identifier content identifier
 * @param array  $data view vars
 * 					To pass in attachments, add a key named 'attachments' - this array
 * 					will be passed to Email::attachments().
 * @param bool $enqueue whether to immediately save and enqueue the notification
 * @return Notification notification entity
 * @throws \InvalidArgumentException Thrown if a non-existant content identifier is given
 */
	public function createNotification($identifier, array $data, $enqueue = false) {
		$data = Hash::merge([
			'locale' => Configure::read('locale'),
			'recipient_user_id' => null,
			'transport' => null,
			'config' => [],
			'locked' => false,
			'send_tries' => 0,
			'sent' => 0,
			'notification_identifier' => $identifier
		], $data);

		$content = TableRegistry::get('Notifications.NotificationContents')->getByIdentifier($identifier, $data['locale']);
		if (!$content) {
			throw new \InvalidArgumentException(__d('notifications', 'create_notification.invalid_identifier', $identifier));
		}

		$notification = $this->newEntity($data);
		if ($enqueue) {
			return $this->enqueue($notification);
		}
		return $notification;
	}

/**
 * Creates notifications for multiple notification transports at once. In this case,
 * $data['transport'] should be an array of strings (e.g. ['email', 'push_message'])
 *
 * @param string $identifier content identifier
 * @param array $data view vars and configuration
 * @param bool $enqueue whether to immediately save and enqueue the notifications
 * @return array Array of Notification instances created
 */
	public function createNotifications($identifier, array $data, $enqueue = false) {
		if (!is_array($data['transport'])) {
			$data = [$data['transport']];
		}
		$notifications = [];
		foreach ($data['transport'] as $transport) {
			$notificationData = $data;
			$notificationData['transport'] = $transport;
			$notifications[] = $this->createNotification($identifier, $notificationData, $enqueue);
		}
		return $notifications;
	}

/**
 * Adds a notification to the queue
 *
 * @param Notification $notification notification entity to be enqueued
 * @return mixed false on failure, the Notification entity on success
 */
	public function enqueue(Notification $notification) {
		$ret = $this->save($notification);
		return $ret;
	}

/**
 * Returns a list of queued notifications that need to be sent
 *
 * @param int $size 	Limit of notifications
 * @return array
 */
	public function getBatch($size = 10) {
		$query = $this->find();
		$query->where([
			'locked' => false,
			'send_tries <' => $this->getMaxSendTries(),
			'sent' => false
		]);<commit_msg>added send_after field and scope on batch<commit_after><?php
namespace Notifications\Model\Table;

use Cake\Core\Configure;
use Cake\I18n\Time;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Cake\Validation\Validator;
use Notifications\Model\Entity\Notification;
use Notifications\Transport\Transport;

/**
 * NotificationQueues Model
 */
class NotificationQueueTable extends Table {

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
	public function initialize(array $config) {
		$this->table('notification_queue');
		$this->displayField('id');
		$this->primaryKey('id');
		$this->addBehavior('Timestamp');

		$this->entityClass('Notifications\Model\Entity\Notification');

		$this->belongsTo('RecipientUsers', [
			'foreignKey' => 'recipient_user_id',
			'className' => 'Users',
		]);

		$this->schema()->columnType('config', 'json');
	}

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator The validator to use when validating the entity.
 * @return \Cake\Validation\Validator
 */
	public function validationDefault(Validator $validator) {
		$validator
			->add('id', 'valid', ['rule' => 'uuid'])
			->allowEmpty('id', 'create')
			->requirePresence('locale', 'create')
			->notEmpty('locale')
			->add('recipient_user_id', 'valid', ['rule' => 'uuid'])
			->requirePresence('recipient_user_id', 'create')
			->notEmpty('recipient_user_id')
			->allowEmpty('notification_identifier')
			->requirePresence('config', 'create')
			->notEmpty('config')
			->requirePresence('transport', 'create')
			->notEmpty('transport')
			->add('locked', 'valid', ['rule' => 'boolean'])
			->allowEmpty('locked')
			->notEmpty('send_tries')
			->allowEmpty('debug');

		return $validator;
	}

/**
 * Creates a Notification entity
 *
 * @param string $identifier content identifier
 * @param array  $data view vars
 * 					To pass in attachments, add a key named 'attachments' - this array
 * 					will be passed to Email::attachments().
 * @param bool $enqueue whether to immediately save and enqueue the notification
 * @return Notification notification entity
 * @throws \InvalidArgumentException Thrown if a non-existant content identifier is given
 */
	public function createNotification($identifier, array $data, $enqueue = false) {
		$data = Hash::merge([
			'locale' => Configure::read('locale'),
			'recipient_user_id' => null,
			'transport' => null,
			'config' => [],
			'locked' => false,
			'send_tries' => 0,
			'sent' => 0,
			'notification_identifier' => $identifier
		], $data);

		$content = TableRegistry::get('Notifications.NotificationContents')->getByIdentifier($identifier, $data['locale']);
		if (!$content) {
			throw new \InvalidArgumentException(__d('notifications', 'create_notification.invalid_identifier', $identifier));
		}

		$notification = $this->newEntity($data);
		if ($enqueue) {
			return $this->enqueue($notification);
		}
		return $notification;
	}

/**
 * Creates notifications for multiple notification transports at once. In this case,
 * $data['transport'] should be an array of strings (e.g. ['email', 'push_message'])
 *
 * @param string $identifier content identifier
 * @param array $data view vars and configuration
 * @param bool $enqueue whether to immediately save and enqueue the notifications
 * @return array Array of Notification instances created
 */
	public function createNotifications($identifier, array $data, $enqueue = false) {
		if (!is_array($data['transport'])) {
			$data = [$data['transport']];
		}
		$notifications = [];
		foreach ($data['transport'] as $transport) {
			$notificationData = $data;
			$notificationData['transport'] = $transport;
			$notifications[] = $this->createNotification($identifier, $notificationData, $enqueue);
		}
		return $notifications;
	}

/**
 * Adds a notification to the queue
 *
 * @param Notification $notification notification entity to be enqueued
 * @return mixed false on failure, the Notification entity on success
 */
	public function enqueue(Notification $notification) {
		$ret = $this->save($notification);
		return $ret;
	}

/**
 * Returns a list of queued notifications that need to be sent
 *
 * @param int $size 	Limit of notifications
 * @return array
 */
	public function getBatch($size = 10) {
		$query = $this->find();
		$query->where([
			'locked' => false,
			'send_tries <' => $this->getMaxSendTries(),
			'sent' => false,
			'OR' => [
				'send_after IS' => null,
				'send_after <=' => Time::now()
			]
		]);
{'fraction_non_alphanumeric': 0.15376906803251308, 'fraction_numerical': 0.0011134617525887986, 'mean_word_length': 4.645505971087366, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 102, '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': '3422313', 'n_tokens_mistral': 2882, 'n_tokens_neox': 2595, 'n_words': 704}