1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 
<?php

namespace SAREhub\Commons\Misc;


/**
 * Time provider for better control time operations with time frezing future.
 */
class TimeProvider
{

    /**
     * @var int|null
     */
    protected $frozenTime = null;

    private static $instance = null;

    /**
     * @return TimeProvider
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Returns current time or frozen time when is defined
     * @return int
     */
    public function now()
    {
        return $this->hasFrezenTime() ? $this->frozenTime : time();
    }

    /**
     * Sets const time, now() call will return 'frozen' time
     * @param null|int $now When null sets time from now() call as 'frozenTime'
     * @return int Returns frozen time value
     */
    public function freezeTime($now = null)
    {
        return $this->frozenTime = ($now === null ? $this->now() : $now);
    }

    /**
     * Unfrezze time, now() call will return current time
     */
    public function unfreezeTime()
    {
        $this->frozenTime = null;
    }

    /**
     * @return bool
     */
    public function hasFrezenTime()
    {
        return $this->frozenTime !== null;
    }

    /**
     * @return null|int
     */
    public function getFrezenTime()
    {
        return $this->frozenTime;
    }


}