Completed
Push — master ( c54dd1...7d6afc )
by Douglas
03:28
created

FileRobotStore   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 53.56%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 0
loc 91
ccs 15
cts 28
cp 0.5356
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 4 1
A getStore() 0 9 2
A createStore() 0 17 3
A getRobot() 0 12 2
A saveRobot() 0 8 1
1
<?php
2
3
/**
4
 * (c) 2018 Douglas Reith.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace Reith\ToyRobot\Infrastructure\Persistence;
12
13
use Reith\ToyRobot\Domain\Robot\Robot;
14
15
class FileRobotStore implements RobotStoreInterface
16
{
17
    private const FILE_VERSION = '001';
18
19
    private static $store;
20
21
    private $file;
22
23
    /**
24
     * __construct
25
     *
26
     * @param \SplFileObject $file
27
     */
28 1
    private function __construct(\SplFileObject $file)
29
    {
30 1
        $this->file = $file;
31 1
    }
32
33
    public function __destruct()
34
    {
35
        $this->file = null;
36
    }
37
38
    /**
39
     * getStore
40
     *
41
     * @param string $basePath
42
     * @return RobotStoreInterface
43
     */
44 1
    public static function getStore(string $basePath): RobotStoreInterface
45
    {
46
        // Ensure the same store is used everywhere
47 1
        if (!self::$store) {
48 1
            self::$store = self::createStore($basePath);
49
        }
50
51 1
        return self::$store;
52
    }
53
54
    /**
55
     * createStore
56
     *
57
     * @param string $basePath
58
     * @return RobotStoreInterface
59
     */
60 1
    private static function createStore(string $basePath): RobotStoreInterface
61
    {
62 1
        $baseDir = $basePath . DIRECTORY_SEPARATOR . 'robotstore';
63
64 1
        if (false === file_exists($baseDir)) {
65 1
            mkdir($baseDir, 0644, true);
66
        }
67
68 1
        $fileName = $baseDir . DIRECTORY_SEPARATOR . self::FILE_VERSION . '-robot.txt';
69
70 1
        if (false === file_exists($fileName)) {
71 1
            touch($fileName);
72
        }
73
74
        // w+ - for reading and writing
75 1
        return new static(new \SplFileObject($fileName), 'w+');
0 ignored issues
show
Unused Code introduced by
The call to FileRobotStore::__construct() has too many arguments starting with 'w+'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
76
    }
77
78
    /**
79
     * @return Robot|null
80
     */
81
    public function getRobot(): ?Robot
82
    {
83
        if (!$this->file->getSize()) {
84
            return null;
85
        }
86
87
        $this->file->rewind();
88
89
        $contents = $this->file->fread($this->file->getSize());
90
91
        return unserialize($contents);
92
    }
93
94
    /**
95
     * @param Robot $robot
96
     */
97
    public function saveRobot(Robot $robot): void
98
    {
99
        // Empty the file
100
        $this->file->ftruncate(0);
101
102
        // Save the robot
103
        $this->file->fwrite(serialize($robot));
104
    }
105
}
106