<commit_before><?php

namespace Interfaces\Api\Controllers;

use Interfaces\Api\ApiController;
use Illuminate\Validation\Factory as Validator;
use Illuminate\Http\Request;
use Domains\Services\PlayerWarnings\PlayerWarningService;

class GameWarningController extends ApiController
{
    /**
     * @var Validator
     */
    private $validationFactory;

    /**
     * @var PlayerWarningService
     */
    private $playerWarningService;


    public function __construct(PlayerWarningService $playerWarningService,
                                Validator $validationFactory) 
    {
        $this->playerWarningService = $playerWarningService;
        $this->validationFactory = $validationFactory;
    }

    public function storeWarning(Request $request)
    {
        $validator = $this->validationFactory->make($request->all(), [
            'player_id_type'    => 'required',
            'player_id'         => 'required|max:60',
            'staff_id_type'     => 'required',
            'staff_id'          => 'required|max:60',
            'reason'            => 'string',
            'weight'            => 'integer',
        ]);

        if ($validator->failed()) {
            throw new BadRequestException('bad_input', $validator->errors()->first());
        }

        // TODO: use ServerKey
        $serverId = 1;

        $warnedPlayerId     = $request->get('player_id');
        $staffPlayerId      = $request->get('staff_id');
        $reason             = $request->get('reason');
        $weight             = $request->get('weight');
        
        $warnedPlayerType   = GameIdentifierType::fromRawValue($request->get('player_id_type'));
        $staffPlayerType    = GameIdentifierType::fromRawValue($request->get('staff_id_type'));

        $warning = $this->playerWarningService->warn($warnedPlayerId,
                                                     $warnedPlayerType->playerType(),
                                                     $staffPlayerId,
                                                     $staffPlayerType->playerType(),
                                                     $reason,
                                                     $serverId,
                                                     $weight);
        // TODO: convert to Resource
        return $warning;
    }

    public function getWarningCount(Request $request)
    {
        $validator = $this->validationFactory->make($request->all(), [
            'player_id_type'    => 'required',
            'player_id'         => 'required|max:60',
        ]);

        if ($validator->failed()) {
            throw new BadRequestException('bad_input', $validator->errors()->first());
        }

        // TODO: use ServerKey
        $serverId = 1;

        $warnedPlayerId     = $request->get('player_id');
        $warnedPlayerType   = GameIdentifierType::fromRawValue($request->get('player_id_type'));

        $count = $this->playerWarningService->getWarningCount($warnedPlayerId,
                                                              $warnedPlayerType->playerType(),
                                                              $serverId);
        // TODO: convert to Resource
        return $count;
    }
}
<commit_msg>Add key checks to warnings<commit_after><?php

namespace Interfaces\Api\Controllers;

use Interfaces\Api\ApiController;
use Illuminate\Validation\Factory as Validator;
use Illuminate\Http\Request;
use Domains\Services\PlayerWarnings\PlayerWarningService;
use Domains\Services\PlayerBans\ServerKeyAuthService;
use Domains\Services\PlayerBans\Exceptions\UnauthorisedKeyActionException;

class GameWarningController extends ApiController
{
    /**
     * @var Validator
     */
    private $validationFactory;

    /**
     * @var PlayerWarningService
     */
    private $playerWarningService;

    /**
     * @var ServerKeyAuthService
     */
    private $serverKeyAuthService;


    public function __construct(PlayerWarningService $playerWarningService,
                                ServerKeyAuthService $serverKeyAuthService,
                                Validator $validationFactory) 
    {
        $this->playerWarningService = $playerWarningService;
        $this->serverKeyAuthService = $serverKeyAuthService;
        $this->validationFactory = $validationFactory;
    }

    public function storeWarning(Request $request)
    {
        $authHeader = $request->header('Authorization');
        $serverKey = $this->serverKeyAuthService->getServerKey($authHeader);

        if (!$serverKey->can_warn) {
            throw new UnauthorisedKeyActionException('unauthorised_action', 'This key does not have permission to create warnings');
        }

        $validator = $this->validationFactory->make($request->all(), [
            'player_id_type'    => 'required',
            'player_id'         => 'required|max:60',
            'staff_id_type'     => 'required',
            'staff_id'          => 'required|max:60',
            'reason'            => 'string',
            'weight'            => 'integer',
        ]);

        if ($validator->failed()) {
            throw new BadRequestException('bad_input', $validator->errors()->first());
        }

        // TODO: use ServerKey
        $serverId = 1;

        $warnedPlayerId     = $request->get('player_id');
        $staffPlayerId      = $request->get('staff_id');
        $reason             = $request->get('reason');
        $weight             = $request->get('weight');
        
        $warnedPlayerType   = GameIdentifierType::fromRawValue($request->get('player_id_type'));
        $staffPlayerType    = GameIdentifierType::fromRawValue($request->get('staff_id_type'));

        $warning = $this->playerWarningService->warn($warnedPlayerId,
                                                     $warnedPlayerType->playerType(),
                                                     $staffPlayerId,
                                                     $staffPlayerType->playerType(),
                                                     $reason,
                                                     $serverId,
                                                     $weight);
        // TODO: convert to Resource
        return $warning;
    }

    public function getWarningCount(Request $request)
    {
        $authHeader = $request->header('Authorization');
        $serverKey = $this->serverKeyAuthService->getServerKey($authHeader);

        if (!$serverKey->can_warn) {
            throw new UnauthorisedKeyActionException('unauthorised_action', 'This key does not have permission to create warnings');
        }
        
        $validator = $this->validationFactory->make($request->all(), [
            'player_id_type'    => 'required',
            'player_id'         => 'required|max:60',
        ]);

        if ($validator->failed()) {
            throw new BadRequestException('bad_input', $validator->errors()->first());
        }

        // TODO: use ServerKey
        $serverId = 1;

        $warnedPlayerId     = $request->get('player_id');
        $warnedPlayerType   = GameIdentifierType::fromRawValue($request->get('player_id_type'));

        $count = $this->playerWarningService->getWarningCount($warnedPlayerId,
                                                              $warnedPlayerType->playerType(),
                                                              $serverId);
        // TODO: convert to Resource
        return $count;
    }
}

{'fraction_non_alphanumeric': 0.114758473445423, 'fraction_numerical': 0.0021350413664264745, 'mean_word_length': 1.7454212454212454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7147382', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1762, 'n_words': 302}