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+'); |
|
|
|
|
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
|
|
|
|
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.