<commit_before><?php

namespace App\Services;

use App\DataTransferObjects\Admin\Category;
use App\DataTransferObjects\Admin\Server as DTO;
use App\Exceptions\Server\AttemptToDeleteTheLastCategoryException;
use App\Exceptions\Server\AttemptToDeleteTheLastServerException;
use App\Repositories\CategoryRepository;
use App\Repositories\ServerRepository;
use Carbon\Carbon;

/**
 * Class Server
 *
 * @author  D3lph1 <<EMAIL>>
 *
 * @package App\Services
 */
class Server
{
    /**
     * @var ServerRepository
     */
    private $serverRepository;

    /**
     * @var CategoryRepository
     */
    private $categoryRepository;

    /**
     * @param ServerRepository   $serverRepository
     * @param CategoryRepository $categoryRepository
     */
    public function __construct(ServerRepository $serverRepository, CategoryRepository $categoryRepository)
    {
        $this->serverRepository = $serverRepository;
        $this->categoryRepository = $categoryRepository;
    }

    /**
     * Enables the server.
     *
     * @param int $serverId Server identifier.
     *
     * @return bool
     */
    public function enableServer($serverId)
    {
        return $this->serverRepository->enable($serverId);
    }

    /**
     * Disables the server.
     *
     * @param int $serverId Server identifier.
     *
     * @return bool
     */
    public function disableServer($serverId)
    {
        return $this->serverRepository->disable($serverId);
    }

    /**
     * Create a new server with attached categories.
     *
     * @param DTO $dto
     */
    public function createServer(DTO $dto)
    {
        \DB::transaction(function () use ($dto) {
            $server = $this->serverRepository->create([
                'name' => $dto->getName(),
                'enabled' => $dto->isEnabled(),
                'ip' => $dto->getIp(),
                'port' => $dto->getPort(),
                'password' => $dto-><PASSWORD>(),
                'monitoring_enabled' => $dto->isMonitoringEnabled()
            ]);

            foreach ($dto->getCategories() as $category) {
                $query = [
                    'name' => $category,
                    'server_id' => $server->id
                ];

                $this->categoryRepository->create($query);
            }
        });
    }

    /**
     * Update given server with categories.
     *
     * @param DTO $dto
     */
    public function updateServer(DTO $dto)
    {
        \DB::transaction(function () use ($dto) {
            $this->serverRepository->update($dto->getId(), [
                'name' => $dto->getName(),
                'enabled' => $dto->isEnabled(),
                'ip' => $dto->getIp(),
                'port' => $dto->getPort(),
                'password' => $dto-><PASSWORD>(),
                'monitoring_enabled' => $dto->isMonitoringEnabled()
            ]);

            foreach ($dto->getCategories() as $category) {<commit_msg>Fixed bug related to rcon password.<commit_after><?php

namespace App\Services;

use App\DataTransferObjects\Admin\Category;
use App\DataTransferObjects\Admin\Server as DTO;
use App\Exceptions\Server\AttemptToDeleteTheLastCategoryException;
use App\Exceptions\Server\AttemptToDeleteTheLastServerException;
use App\Repositories\CategoryRepository;
use App\Repositories\ServerRepository;
use Carbon\Carbon;

/**
 * Class Server
 *
 * @author  D3lph1 <<EMAIL>>
 *
 * @package App\Services
 */
class Server
{
    /**
     * @var ServerRepository
     */
    private $serverRepository;

    /**
     * @var CategoryRepository
     */
    private $categoryRepository;

    /**
     * @param ServerRepository   $serverRepository
     * @param CategoryRepository $categoryRepository
     */
    public function __construct(ServerRepository $serverRepository, CategoryRepository $categoryRepository)
    {
        $this->serverRepository = $serverRepository;
        $this->categoryRepository = $categoryRepository;
    }

    /**
     * Enables the server.
     *
     * @param int $serverId Server identifier.
     *
     * @return bool
     */
    public function enableServer($serverId)
    {
        return $this->serverRepository->enable($serverId);
    }

    /**
     * Disables the server.
     *
     * @param int $serverId Server identifier.
     *
     * @return bool
     */
    public function disableServer($serverId)
    {
        return $this->serverRepository->disable($serverId);
    }

    /**
     * Create a new server with attached categories.
     *
     * @param DTO $dto
     */
    public function createServer(DTO $dto)
    {
        \DB::transaction(function () use ($dto) {
            $server = $this->serverRepository->create([
                'name' => $dto->getName(),
                'enabled' => $dto->isEnabled(),
                'ip' => $dto->getIp(),
                'port' => $dto->getPort(),
                'password' => $dto-><PASSWORD>(),
                'monitoring_enabled' => $dto->isMonitoringEnabled()
            ]);

            foreach ($dto->getCategories() as $category) {
                $query = [
                    'name' => $category->getName(),
                    'server_id' => $server->id
                ];

                $this->categoryRepository->create($query);
            }
        });
    }

    /**
     * Update given server with categories.
     *
     * @param DTO $dto
     */
    public function updateServer(DTO $dto)
    {
        \DB::transaction(function () use ($dto) {
            $this->serverRepository->update($dto->getId(), [
                'name' => $dto->getName(),
                'enabled' => $dto->isEnabled(),
                'ip' => $dto->getIp(),
                'port' => $dto->getPort(),
                'password' => $<PASSWORD>(),
                'monitoring_enabled' => $dto->isMonitoringEnabled()
            ]);

            foreach ($dto->getCategories() as $category) {
{'fraction_non_alphanumeric': 0.1376566203860481, 'fraction_numerical': 0.0006772773450728074, 'mean_word_length': 2.0183955033214103, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 95, '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': '6164624', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1612, 'n_words': 315}