<commit_before><?php

namespace UniEngine\Engine\Modules\FlightControl\Screens\Shortcuts\Commands;

/**
 * @param Object $props
 * @param Object $props['input']
 * @param String $props['userId']
 * @param String $props['shortcutId']
 *
 * @return Object $result
 * @return Boolean $result['isSuccess']
 */
function upsertShortcut($props) {
    $input = $props['input'];
    $userId = $props['userId'];
    $shortcutId = (
        isset($props['shortcutId']) ?
            $props['shortcutId'] :
            null
    );

    $normalizedInput['customName'] = trim($input['name']);
    $normalizedInput['galaxy'] = intval($input['galaxy']);
    $normalizedInput['system'] = intval($input['system']);
    $normalizedInput['planet'] = intval($input['planet']);
    $normalizedInput['planetType'] = intval($input['type']);
    $normalizedInput['targetId'] = '0';

    if ($shortcutId !== null) {
        $shortcutId = intval($shortcutId, 10);

        if ($shortcutId <= 0) {
            return [
                'isSuccess' => false,
                'error' => [
                    'code' => 'INVALID_ID',
                ],
            ];
        }

        $fetchShortcutQuery = (
            "SELECT " .
            "`id_owner` " .
            "FROM {{table}} " .
            "WHERE " .
            "`id` = {$shortcutId} " .
            "LIMIT 1 " .
            ";"
        );
        $shortcutEntry = doquery($fetchShortcutQuery, 'fleet_shortcuts', true);

        if (!$shortcutEntry) {
            return [
                'isSuccess' => false,
                'error' => [
                    'code' => 'INVALID_ID',
                ],
            ];
        }
        if ($shortcutEntry['id_owner'] != $userId) {
            return [
                'isSuccess' => false,
                'error' => [
                    'code' => 'USER_NOT_OWNER',
                ],
            ];
        }
    }

    if (
        !in_array(
            $normalizedInput['planetType'],
            [ 1, 2, 3 ]
        ) ||
        $normalizedInput['galaxy'] <= 0 ||
        $normalizedInput['galaxy'] > MAX_GALAXY_IN_WORLD ||
        $normalizedInput['system'] <= 0 ||
        $normalizedInput['system'] > MAX_SYSTEM_IN_GALAXY ||
        $normalizedInput['planet'] <= 0 ||
        $normalizedInput['planet'] > (MAX_PLANET_IN_SYSTEM + 1)
    ) {
        return [
            'isSuccess' => false,
            'error' => [
                'code' => 'INVALID_COORDINATES',
            ],
        ];
    }

    if (
        !empty($normalizedInput['customName']) &&
        !preg_match('/^[a-zA-Z0-9\_\-\ ]{1,}$/D', $normalizedInput['customName'])
    ) {
        return [
            'isSuccess' => false,
            'error' => [<commit_msg>GH-139 Use coords validator when upserting shortcuts<commit_after><?php

namespace UniEngine\Engine\Modules\FlightControl\Screens\Shortcuts\Commands;

use UniEngine\Engine\Modules\Flights;

/**
 * @param Object $props
 * @param Object $props['input']
 * @param String $props['userId']
 * @param String $props['shortcutId']
 *
 * @return Object $result
 * @return Boolean $result['isSuccess']
 */
function upsertShortcut($props) {
    $input = $props['input'];
    $userId = $props['userId'];
    $shortcutId = (
        isset($props['shortcutId']) ?
            $props['shortcutId'] :
            null
    );

    $normalizedInput['customName'] = trim($input['name']);
    $normalizedInput['galaxy'] = intval($input['galaxy']);
    $normalizedInput['system'] = intval($input['system']);
    $normalizedInput['planet'] = intval($input['planet']);
    $normalizedInput['planetType'] = intval($input['type']);
    $normalizedInput['targetId'] = '0';

    if ($shortcutId !== null) {
        $shortcutId = intval($shortcutId, 10);

        if ($shortcutId <= 0) {
            return [
                'isSuccess' => false,
                'error' => [
                    'code' => 'INVALID_ID',
                ],
            ];
        }

        $fetchShortcutQuery = (
            "SELECT " .
            "`id_owner` " .
            "FROM {{table}} " .
            "WHERE " .
            "`id` = {$shortcutId} " .
            "LIMIT 1 " .
            ";"
        );
        $shortcutEntry = doquery($fetchShortcutQuery, 'fleet_shortcuts', true);

        if (!$shortcutEntry) {
            return [
                'isSuccess' => false,
                'error' => [
                    'code' => 'INVALID_ID',
                ],
            ];
        }
        if ($shortcutEntry['id_owner'] != $userId) {
            return [
                'isSuccess' => false,
                'error' => [
                    'code' => 'USER_NOT_OWNER',
                ],
            ];
        }
    }

    $isValidCoordinate = Flights\Utils\Checks\isValidCoordinate([
        'coordinate' => [
            'galaxy' => $normalizedInput['galaxy'],
            'system' => $normalizedInput['system'],
            'planet' => $normalizedInput['planet'],
            'type' => $normalizedInput['planetType'],
        ]
    ]);

    if (!$isValidCoordinate['isValid']) {
        return [
            'isSuccess' => false,
            'error' => [
                'code' => 'INVALID_COORDINATES',
            ],
        ];
    }

    if (
        !empty($normalizedInput['customName']) &&
        !preg_match('/^[a-zA-Z0-9\_\-\ ]{1,}$/D', $normalizedInput['customName'])
    ) {
        return [
            'isSuccess' => false,
            'error' => [
{'fraction_non_alphanumeric': 0.1711777615215801, 'fraction_numerical': 0.004754937820043892, 'mean_word_length': 1.7168405365126678, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 39, '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': '1466127', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1623, 'n_words': 248}