Completed
Push — master ( 956692...a443d7 )
by Tomasz
02:25
created

FixedConfig::heartbeat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Fixed configuration.
4
 * 
5
 * This is designed to be used where each machine **knows** what its machine
6
 * ID is - eg: via some kind of automatically deployed configuration
7
 * (puppet etc.)
8
 * 
9
 * @author @davegardnerisme
10
 */
11
12
namespace Gendoria\CruftFlake\Config;
13
14
use Psr\Log\LoggerAwareInterface;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\NullLogger;
17
18
class FixedConfig implements ConfigInterface, LoggerAwareInterface
19
{
20
    /**
21
     * Machine ID.
22
     * 
23
     * @var int
24
     */
25
    private $machineId;
26
27
    /**
28
     * Logger.
29
     * 
30
     * @var LoggerInterface
31
     */
32
    private $logger;
33
34
    /**
35
     * Constructor.
36
     * 
37
     * @param int             $machineId Fixed machine ID.
38
     * @param LoggerInterface $logger    Logger class
39
     */
40 5
    public function __construct($machineId, LoggerInterface $logger = null)
41
    {
42 5
        $this->machineId = (int) $machineId;
43 5
        if ($logger) {
44 1
            $this->logger = $logger;
45 1
        } else {
46 4
            $this->logger = new NullLogger();
47
        }
48 5
    }
49
50
    /**
51
     * Get machine identifier.
52
     * 
53
     * @return int Should be a 10-bit int (decimal 0 to 1023)
54
     */
55 5
    public function getMachine()
56
    {
57 5
        $this->logger->debug('Obtained machine ID '.$this->machineId.' through fixed configuration.');
58
59 5
        return $this->machineId;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     * 
65
     * This function will always return false, as fixed config does not resync machine ID.
66
     */
67 1
    public function heartbeat()
68
    {
69 1
        return false;
70
    }
71
72
    /**
73
     * Set logger.
74
     * 
75
     * @param LoggerInterface $logger
76
     */
77 1
    public function setLogger(LoggerInterface $logger)
78
    {
79 1
        $this->logger = $logger;
80 1
    }
81
}
82