FixedConfig::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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