Completed
Push — master ( fbaba0...9f3a52 )
by Tomasz
02:45
created

FixedConfig::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 9.6666
cc 2
eloc 6
nc 2
nop 2
crap 2.0932
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 3
    public function __construct($machineId, LoggerInterface $logger = null)
41
    {
42 3
        $this->machineId = (int) $machineId;
43 3
        if ($logger) {
44
            $this->logger = $logger;
45
        } else {
46 3
            $this->logger = new NullLogger();
47
        }
48 3
    }
49
50
    /**
51
     * Get machine identifier.
52
     * 
53
     * @return int Should be a 10-bit int (decimal 0 to 1023)
54
     */
55 3
    public function getMachine()
56
    {
57 3
        $this->logger->debug('Obtained machine ID '.$this->machineId.' through fixed configuration.');
58
59 3
        return $this->machineId;
60
    }
61
62
    /**
63
     * Set logger.
64
     * 
65
     * @param LoggerInterface $logger
66
     */
67 1
    public function setLogger(LoggerInterface $logger)
68
    {
69 1
        $this->logger = $logger;
70 1
    }
71
}
72