Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
57.14% covered (warning)
57.14%
4 / 7
CRAP
72.22% covered (success)
72.22%
13 / 18
SequencerFactory
0.00% covered (danger)
0.00%
0 / 1
57.14% covered (warning)
57.14%
4 / 7
10.74
72.22% covered (success)
72.22%
13 / 18
 getInstance
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 nextSequence
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 registerSequencer
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 addSequencer
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getSequencer
0.00% covered (danger)
0.00%
0 / 1
2.06
75.00% covered (success)
75.00%
3 / 4
 getNextSequence
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
<?php
declare(strict_types = 1);
namespace Siesta\Sequencer;
use Siesta\Util\ArrayUtil;
class SequencerFactory
{
    const ERROR_SEQUENCER_NOT_REGISTERED = "Sequencer '%s' is not registered.";
    /**
     * @var SequencerFactory
     */
    private static $instance;
    /**
     * @return SequencerFactory
     */
    public static function getInstance()
    {
        if (self::$instance === null) {
            self::$instance = new SequencerFactory();
        }
        return self::$instance;
    }
    /**
     * @param string $sequencerName
     * @param string $tableName
     * @param string|null $connectionName
     *
     * @return mixed
     */
    public static function nextSequence(string $sequencerName, string $tableName, string $connectionName = null)
    {
        return self::getInstance()->getNextSequence($sequencerName, $tableName, $connectionName);
    }
    /**
     * @param string $name
     * @param Sequencer $sequencer
     */
    public static function registerSequencer(string $name, Sequencer $sequencer)
    {
        self::getInstance()->addSequencer($name, $sequencer);
    }
    /**
     * @var Sequencer[]
     */
    protected $sequencerList;
    /**
     * SequenceFactory constructor.
     */
    protected function __construct()
    {
        $this->sequencerList = [];
        $this->sequencerList[UUIDSequencer::NAME] = new UUIDSequencer();
        $this->sequencerList[AutoincrementSequencer::NAME] = new AutoincrementSequencer();
    }
    /**
     * @param string $name
     * @param Sequencer $sequencer
     */
    public function addSequencer(string $name, Sequencer $sequencer)
    {
        $this->sequencerList[$name] = $sequencer;
    }
    /**
     * @param string $sequencerName
     *
     * @return null|Sequencer
     */
    protected function getSequencer(string $sequencerName) : Sequencer
    {
        $sequencer = ArrayUtil::getFromArray($this->sequencerList, $sequencerName);
        if ($sequencer === null) {
            throw new \RuntimeException(sprintf(self::ERROR_SEQUENCER_NOT_REGISTERED, $sequencerName));
        }
        return $sequencer;
    }
    /**
     * @param string $sequencerName
     * @param string $tableName
     * @param string|null $connectionName
     *
     * @return mixed
     */
    public function getNextSequence(string $sequencerName, string $tableName, string $connectionName = null)
    {
        $sequencer = $this->getSequencer($sequencerName);
        return $sequencer->getNextSequence($tableName, $connectionName);
    }
}