garethellis36/crontab-schedule-generator

View on GitHub
src/Hourly.php

Summary

Maintainability
A
30 mins
Test Coverage
<?php

declare(strict_types=1);

namespace Garethellis\CrontabScheduleGenerator;

use Assert\Assertion;

class Hourly
{
    private $mins = 0;

    private $textDescriptions = [
        "on the hour"  => "0",
        "quarter past" => "15",
        "half past"    => "30",
        "quarter to"   => "45",
    ];

    public function __toString()
    {
        if (!is_array($this->mins)) {
            return sprintf("%s * * * *", $this->mins);
        }
        return sprintf("%s * * * *", implode(",", $this->mins));
    }

    public function at(string $minutes): self
    {
        if (!is_numeric($minutes)) {
            Assertion::choice($minutes, array_keys($this->textDescriptions));
            $minutes = $this->textDescriptions[$minutes];
        } else {
            Assertion::range($minutes, 0, 59);
        }

        $this->mins = $minutes;
        return $this;
    }

    public function repeatingAt(string $minutes): self
    {
        if (!is_numeric($minutes)) {
            Assertion::choice($minutes, array_keys($this->textDescriptions));
            $minutes = $this->textDescriptions[$minutes];
        } else {
            Assertion::range($minutes, 0, 59);
        }

        if (!is_array($this->mins)) {
            $this->mins = [$this->mins];
        }

        if (!in_array($minutes, $this->mins)) {
            $this->mins[] = $minutes;
        }
        return $this;
    }
}