6
6
7
7
use Baldwin \UrlDataIntegrityChecker \Exception \SerializationException ;
8
8
use Magento \Framework \App \Config \ScopeConfigInterface ;
9
- use Magento \Store \Model \ScopeInterface ;
10
9
use Magento \Framework \App \Filesystem \DirectoryList ;
11
10
use Magento \Framework \Exception \FileSystemException ;
12
11
use Magento \Framework \Filesystem ;
12
+ use Magento \Framework \Filesystem \Directory \ReadFactory ;
13
+ use Magento \Framework \Filesystem \Directory \WriteFactory ;
14
+ use Magento \Framework \Filesystem \Driver \File as FileDriver ;
13
15
14
16
class FileStorage extends AbstractStorage implements StorageInterface
15
17
{
16
- const CONFIG_PATH = 'url/checker/path ' ;
18
+ const CONFIG_PATH = 'url_data_integrity_checker/configuration/filestorage_path ' ;
17
19
18
20
private $ filesystem ;
19
21
private $ scopeConfig ;
22
+ private $ writeFactory ;
23
+ private $ readFactory ;
24
+ private $ fileDriver ;
20
25
21
26
public function __construct (
22
27
Filesystem $ filesystem ,
23
- ScopeConfigInterface $ scopeConfig
28
+ ScopeConfigInterface $ scopeConfig ,
29
+ WriteFactory $ writeFactory ,
30
+ ReadFactory $ readFactory ,
31
+ FileDriver $ fileDriver
24
32
) {
25
33
$ this ->filesystem = $ filesystem ;
26
34
$ this ->scopeConfig = $ scopeConfig ;
35
+ $ this ->writeFactory = $ writeFactory ;
36
+ $ this ->readFactory = $ readFactory ;
37
+ $ this ->fileDriver = $ fileDriver ;
27
38
}
28
39
29
40
public function write (string $ identifier , array $ data ): bool
30
41
{
31
- $ directory = $ this ->filesystem -> getDirectoryWrite ($ this ->getPath () ?: DirectoryList:: TMP );
42
+ $ directory = $ this ->writeFactory -> create ($ this ->getPath ());
32
43
$ directory ->create ();
33
44
$ filename = $ this ->getFilename ($ identifier );
34
45
@@ -46,7 +57,7 @@ public function write(string $identifier, array $data): bool
46
57
47
58
public function read (string $ identifier ): array
48
59
{
49
- $ directory = $ this ->filesystem -> getDirectoryRead ($ this ->getPath () ?: DirectoryList:: TMP );
60
+ $ directory = $ this ->readFactory -> create ($ this ->getPath ());
50
61
$ filename = $ this ->getFilename ($ identifier );
51
62
52
63
$ data = '' ;
@@ -67,9 +78,31 @@ private function getFilename(string $identifier): string
67
78
68
79
private function getPath (): string
69
80
{
70
- return $ this ->scopeConfig ->getValue (
71
- self ::CONFIG_PATH ,
72
- ScopeInterface::SCOPE_STORE
81
+ $ path = $ this ->scopeConfig ->getValue (
82
+ self ::CONFIG_PATH
73
83
);
84
+
85
+ if ($ path === '' || $ path === null ) {
86
+ $ path = 'var/tmp ' ;
87
+ }
88
+
89
+ if ($ this ->fileDriver ->getRealPath ($ path ) === false ) {
90
+ $ rootDir = $ this ->filesystem ->getDirectoryRead (DirectoryList::ROOT )->getAbsolutePath ();
91
+ $ path = $ rootDir . '/ ' . $ path ;
92
+
93
+ if ($ this ->fileDriver ->getRealPath ($ path ) === false ) {
94
+ $ path = $ this ->filesystem ->getDirectoryRead (DirectoryList::TMP )->getAbsolutePath ();
95
+ }
96
+ } else {
97
+ $ path = $ this ->fileDriver ->getRealPath ($ path );
98
+ // hack: these next 3 lines won't ever trigger in real life, but is to satisfy phpstan
99
+ // (I think this is a bug in phpstan)
100
+ if ($ path === true ) {
101
+ $ path = '' ;
102
+ }
103
+ // end hack
104
+ }
105
+
106
+ return $ path ;
74
107
}
75
108
}
0 commit comments