Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
IntegerIdFactory
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInstance
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setNextId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getNextId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\struct\types\id;
10
11use pvc\struct\types\err\SetNextIdException;
12
13/**
14 * Class NodeIdFactory
15 */
16class IntegerIdFactory
17{
18    protected static IntegerIdFactory $instance;
19
20    /**
21     * @var non-negative-int
22     */
23    protected static int $nextId = 0;
24
25    /**
26     * make it a singleton
27     */
28    protected function __construct() {}
29
30    public static function getInstance(): IntegerIdFactory
31    {
32        if (!isset(self::$instance)) {
33            self::$instance = new IntegerIdFactory();
34        }
35        return self::$instance;
36    }
37
38    /**
39     * setNextId
40     * @param  non-negative-int  $id
41     *
42     * @return void
43     */
44    public function setNextId(int $id): void
45    {
46        if ($id < 0) {
47            throw new SetNextIdException();
48        }
49        self::$nextId = $id;
50    }
51
52    /**
53     * getNextId
54     * @return non-negative-int
55     */
56    public static function getNextId(): int
57    {
58        return self::$nextId++;
59    }
60}