<commit_before><?php

namespace Tomloprod\IonicApi\Api;

/**
 * Class Notifications
 *
 * Stores ionic push api methods related to notifications collection.
 * More info: https://docs.ionic.io/api/endpoints/push.html
 *
 * @package Tomloprod\IonicApi\Api
 * @author <NAME> (@tomloprod)
 * @author <NAME> (@ramoncarreras)
 */
class Notifications extends Request {

    public $requestData = [];
    private $ionicProfile;
    private static $endPoints = [
        'list' => '/push/notifications', // GET
        'create' => '/push/notifications', // POST
        'retrieve' => '/push/notifications,:notification_id', // GET
        'replace' => '/push/notifications/:notification_id', // PUT
        'delete' => '/push/notifications/:notification_id', // DELETE
        'listMessages' => '/push/notifications/:notification_id/messages', // GET
    ];

    /**
     * Notifications constructor.
     *
     * @param string $ionicProfile
     * @param string $ionicAPIToken
     */
    public function __construct($ionicProfile, $ionicAPIToken)
    {
        parent::__construct($ionicProfile, $ionicAPIToken);
        $this->ionicProfile = $ionicProfile;
        $this->requestData = ['profile' => $this->ionicProfile];
    }

    /**
     * Set notification config.
     *
     * @param array $notificationData
     * @param array $payloadData - Custom extra data
     * @param bool $silentNotification - Determines if the message should be delivered as a silent notification.
     * @param string $dateTime - Time to start delivery of the notification Y-m-d H:i:s format
     */
    public function setConfig($notificationData, $payloadData = [], $silentNotification = false, $dateTime = '') {
        if (!is_array($notificationData)) {
            $notificationData = [$notificationData];
        }
        if (count($notificationData) > 0) {
            $this->requestData = array_merge($this->requestData, ['notification' => $notificationData]);
        }

        // payload
        if (!is_array($payloadData)) {
            $payloadData = [$payloadData];
        }
        if (count($payloadData) > 0) {
            $this->requestData['notification']['payload'] = $payloadData;
        }

        // silent
        if ($silentNotification) {
            $this->requestData['notification']['android']['content_available'] = 1;
            $this->requestData['notification']['ios']['content_available'] = 1;
        } else {
            unset($this->requestData['notification']['android']['content_available']);
            unset($this->requestData['notification']['ios']['content_available']);
        }

        // scheduled
        if($this->isDatetime($dateTime)) {
            // Convert dateTime to RFC3339
            $this->requestData['scheduled'] = date("c", strtotime($dateTime));
        }
    }

    /**
     * Paginated listing of Push Notifications.
     *
     * @param array $parameters
     * @return array
     */
    public function paginatedList($parameters) {
        $response =  $this->sendRequest(self::METHOD_GET, self::$endPoints['list'] . '?' . http_build_query($parameters), $this->requestData);
        $this->resetRequestData();
        return $response;
    }

    /**
     * Get a Notification.
     *
     * @param string $notificationId - Notification id
     * @return array
     */
    public function retrieve($notificationId) {
        $response = $this->sendRequest(
            self::METHOD_GET,
            str_replace(':notification_id', $notificationId, self::$endPoints['retrieve']),
            $this->requestData
        );
        $this->resetRequestData();
        return $response;
    }

    // TODO: replace

    /**
     * Deletes a notification.
     *
     * @param $notificationId
     * @return array
     */
    public function delete($notificationId) {
        return $this->sendRequest(
            self::METHOD_DELETE,
            str_replace(':notification_id', $notificationId, self::$endPoints['delete'])
        );
    }

    /**
     * List messages of the indicated notification.
     *
     * @param string $notificationId - Notification id
     * @param array $parameters
     * @return array
     */
    public function listMessages($notificationId, $parameters) {
        $endPoint = str_replace(':notification_id', $notificationId, self::$endPoints['listMessages']);
        $response =  $this->sendRequest(self::METHOD_GET, $endPoint . '?' . http_build_query($parameters), $this->requestData);
        $this->resetRequestData();
        return $response;
    }

    /**
     * Send push notification for the indicated device tokens.
     *
     * @param array $deviceTokens
     * @return array
     */
    public function sendNotification($deviceTokens) {
        $this->requestData['tokens'] = $deviceTokens;
        $this->requestData['send_to_all'] = false;
        return $this->create();
    }

    /**
     * Send push notificatoin for all registered devices.
     *
     * @return array
     */
    public function sendNotificationToAll() {
        $this->requestData['send_to_all'] = true;
        return $this->create();
    }

    /**
     * Create a Push Notification.
     *
     * Used by "sendNotification" and "sendNotificationToAll".
     *
     * @private
     * @return array
     */
    private function create() {
        $response = $this->sendRequest(self::METHOD_POST, self::$endPoints['create'], $this->requestData);
        $this->resetRequestData();<commit_msg>Improve readability<commit_after><?php

namespace Tomloprod\IonicApi\Api;

/**
 * Class Notifications
 *
 * Stores ionic push api methods related to notifications collection.
 * More info: https://docs.ionic.io/api/endpoints/push.html
 *
 * @package Tomloprod\IonicApi\Api
 * @author <NAME> (@tomloprod)
 * @author <NAME> (@ramoncarreras)
 */
class Notifications extends Request {

    public $requestData = [];
    private $ionicProfile;
    private static $endPoints = [
        'list' => '/push/notifications', // GET
        'create' => '/push/notifications', // POST
        'retrieve' => '/push/notifications,:notification_id', // GET
        'replace' => '/push/notifications/:notification_id', // PUT
        'delete' => '/push/notifications/:notification_id', // DELETE
        'listMessages' => '/push/notifications/:notification_id/messages', // GET
    ];

    /**
     * Notifications constructor.
     *
     * @param string $ionicProfile
     * @param string $ionicAPIToken
     */
    public function __construct($ionicProfile, $ionicAPIToken)
    {
        parent::__construct($ionicProfile, $ionicAPIToken);
        $this->ionicProfile = $ionicProfile;
        $this->requestData = ['profile' => $this->ionicProfile];
    }

    /**
     * Set notification config.
     *
     * @param array $notificationData
     * @param array $payloadData - Custom extra data
     * @param bool $silentNotification - Determines if the message should be delivered as a silent notification.
     * @param string $dateTime - Time to start delivery of the notification Y-m-d H:i:s format
     */
    public function setConfig($notificationData, $payloadData = [], $silentNotification = false, $dateTime = '') {
        if (!is_array($notificationData)) {
            $notificationData = [$notificationData];
        }
        if (count($notificationData) > 0) {
            $this->requestData = array_merge($this->requestData, ['notification' => $notificationData]);
        }

        // payload
        if (!is_array($payloadData)) {
            $payloadData = [$payloadData];
        }
        if (count($payloadData) > 0) {
            $this->requestData['notification']['payload'] = $payloadData;
        }

        // silent
        if ($silentNotification) {
            $this->requestData['notification']['android']['content_available'] = 1;
            $this->requestData['notification']['ios']['content_available'] = 1;
        } else {
            unset($this->requestData['notification']['android']['content_available']);
            unset($this->requestData['notification']['ios']['content_available']);
        }

        // scheduled
        if($this->isDatetime($dateTime)) {
            // Convert dateTime to RFC3339
            $this->requestData['scheduled'] = date("c", strtotime($dateTime));
        }
    }

    /**
     * Paginated listing of Push Notifications.
     *
     * @param array $parameters
     * @return array
     */
    public function paginatedList($parameters) {
        $response =  $this->sendRequest(
            self::METHOD_GET, 
            self::$endPoints['list'] . '?' . http_build_query($parameters), 
            $this->requestData
        );
        $this->resetRequestData();
        return $response;
    }

    /**
     * Get a Notification.
     *
     * @param string $notificationId - Notification id
     * @return array
     */
    public function retrieve($notificationId) {
        $response = $this->sendRequest(
            self::METHOD_GET,
            str_replace(':notification_id', $notificationId, self::$endPoints['retrieve']),
            $this->requestData
        );
        $this->resetRequestData();
        return $response;
    }

    // TODO: replace

    /**
     * Deletes a notification.
     *
     * @param $notificationId
     * @return array
     */
    public function delete($notificationId) {
        return $this->sendRequest(
            self::METHOD_DELETE,
            str_replace(':notification_id', $notificationId, self::$endPoints['delete'])
        );
    }

    /**
     * List messages of the indicated notification.
     *
     * @param string $notificationId - Notification id
     * @param array $parameters
     * @return array
     */
    public function listMessages($notificationId, $parameters) {
        $endPoint = str_replace(':notification_id', $notificationId, self::$endPoints['listMessages']);
        $response =  $this->sendRequest(
            self::METHOD_GET, 
            $endPoint . '?' . http_build_query($parameters), 
            $this->requestData
        );
        $this->resetRequestData();
        return $response;
    }

    /**
     * Send push notification for the indicated device tokens.
     *
     * @param array $deviceTokens
     * @return array
     */
    public function sendNotification($deviceTokens) {
        $this->requestData['tokens'] = $deviceTokens;
        $this->requestData['send_to_all'] = false;
        return $this->create();
    }

    /**
     * Send push notificatoin for all registered devices.
     *
     * @return array
     */
    public function sendNotificationToAll() {
        $this->requestData['send_to_all'] = true;
        return $this->create();
    }

    /**
     * Create a Push Notification.
     *
     * Used by "sendNotification" and "sendNotificationToAll".
     *
     * @private
     * @return array
     */
    private function create() {
        $response = $this->sendRequest(
            self::METHOD_POST, 
            self::$endPoints['create'], 
            $this->requestData
        );
        $this->resetRequestData();
{'fraction_non_alphanumeric': 0.13932503158274678, 'fraction_numerical': 0.0014437827107020393, 'mean_word_length': 2.4483509645301806, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 87, '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': '1277078', 'n_tokens_mistral': 3045, 'n_tokens_neox': 2967, 'n_words': 625}