<commit_before><?php

namespace OpenDominion\Services;

use Carbon\Carbon;
use OpenDominion\Traits\DominionAwareTrait;

class DominionProtectionService
{
    use DominionAwareTrait;

    const PROTECTION_DURATION_IN_HOURS = 72; // todo: move to config?

    /**
     * Returns the Dominion's 'under protection' start date.
     *
     * @return Carbon
     */
    public function getProtectionStartDate()
    {
        $roundStartDate = Carbon::parse($this->dominion->round->start_date);
        $dominionCreatedDate = Carbon::parse($this->dominion->created_at);

        $protectionStartDate = (($dominionCreatedDate > $roundStartDate) ? $dominionCreatedDate : $roundStartDate);

        return Carbon::parse($protectionStartDate->format('Y-m-d H:00:00'));
    }

    /**
     * Returns the Dominion's 'under protection' end date.
     *
     * @return Carbon
     */
    public function getProtectionEndDate()
    {
        return $this->getProtectionStartDate()->addHours(self::PROTECTION_DURATION_IN_HOURS + 1);
    }

    /**
     * Returns whether this Dominion instance is under protection.
     *
     * @return bool
     */
    public function isUnderProtection()
    {
        return ($this->getProtectionEndDate() >= Carbon::now());
    }

    /**
     * Returns the hours the Dominion is still under protection for.
     *
     * @return int
     */
    public function getUnderProtectionHoursLeft()
    {
        if (!$this->isUnderProtection()) {
            return 0;
        }

        return $this->getProtectionEndDate()->diffInHours(Carbon::now());
    }
}<commit_msg>Working on dominion protection service<commit_after><?php

namespace OpenDominion\Services;

use Carbon\Carbon;
use OpenDominion\Traits\DominionAwareTrait;

class DominionProtectionService
{
    use DominionAwareTrait;

    const PROTECTION_DURATION_IN_HOURS = 72; // todo: move to config?

    /**
     * Returns the Dominion's 'under protection' start date.
     *
     * @return Carbon
     */
    public function getProtectionStartDate()
    {
        $roundStartDate = Carbon::parse($this->dominion->round->start_date);
        $dominionCreatedDate = Carbon::parse($this->dominion->created_at);

        return (($dominionCreatedDate > $roundStartDate) ? $dominionCreatedDate : $roundStartDate);
    }

    /**
     * Returns the Dominion's 'under protection' end date.
     *
     * @return Carbon
     */
    public function getProtectionEndDate()
    {
        $modifiedStartDate = Carbon::parse($this->getProtectionStartDate()->format('Y-m-d H:00:00'));

        return $modifiedStartDate->addHours(self::PROTECTION_DURATION_IN_HOURS);
    }

    /**
     * Returns whether this Dominion instance is under protection.
     *
     * @return bool
     */
    public function isUnderProtection()
    {
        return ($this->getProtectionEndDate() >= Carbon::now());
    }

    /**
     * Returns the hours the Dominion is still under protection for.
     *
     * @return float
     */
    public function getUnderProtectionHoursLeft()
    {
        if (!$this->isUnderProtection()) {
            return 0;
        }

        $minutes = (int)Carbon::now()->format('i');
        $seconds = (int)Carbon::now()->format('s');

        $fraction = (1 - ((($minutes * 60) + $seconds) / 3600));

        return ($this->getProtectionEndDate()->diffInHours(Carbon::now()) + $fraction);
    }
}
{'fraction_non_alphanumeric': 0.12788632326820604, 'fraction_numerical': 0.006512729425695678, 'mean_word_length': 2.8397727272727273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '2128609', 'n_tokens_mistral': 1057, 'n_tokens_neox': 996, 'n_words': 202}