StoragePersister::_getFileName()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/embedi
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 *
11
 */
12
13
namespace Maslosoft\EmbeDi;
14
15
use Maslosoft\EmbeDi\Interfaces\MassAssignedInterface;
16
17
/**
18
 * StoragePersister
19
 *
20
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
21
 */
22
class StoragePersister
23
{
24
25
	/**
26
	 * Mass assigned storage interface
27
	 * @var MassAssignedInterface
28
	 */
29
	private $storage = null;
30
31
	/**
32
	 * Peristence path
33
	 * @var string
34
	 */
35
	private $path = '';
36
37 1
	public function __construct(MassAssignedInterface $storage, $path)
38
	{
39 1
		$this->storage = $storage;
40 1
		$this->path = $path;
41 1
	}
42
43
	/**
44
	 * Save data to disk
45
	 */
46 1
	public function save()
47
	{
48 1
		$data = $this->storage->getAll();
49 1
		$code = sprintf("<?php\n\nreturn %s;", var_export($data, true));
50 1
		file_put_contents($this->_getFileName(), $code);
51 1
	}
52
53
	/**
54
	 * Read data from disk
55
	 */
56 1
	public function load()
57
	{
58
		// file exists not used here as it will exists in next run anyway
59 1
		$data = (array)@include $this->_getFileName();
60 1
		$this->storage->setAll($data);
61 1
	}
62
63
	/**
64
	 * Get storage filename
65
	 * @return string
66
	 */
67 1
	private function _getFileName()
68
	{
69 1
		return rtrim($this->path, '\\/') . '/' . str_replace('\\', '.', sprintf('%s-%s.php', get_class($this), get_class($this->storage)));
70
	}
71
72
}
73