Skip to content

Commit 5966044

Browse files
committed
Reworked PR-28 a bit to make it more logical.
1 parent cdc6a83 commit 5966044

File tree

5 files changed

+58
-28
lines changed

5 files changed

+58
-28
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ checkquality:
2323
xmllint --noout --schema vendor/magento/module-store/etc/config.xsd etc/config.xml
2424
xmllint --noout --schema vendor/magento/module-backend/etc/menu.xsd etc/adminhtml/menu.xml
2525
xmllint --noout --schema vendor/magento/framework/App/etc/routes.xsd etc/adminhtml/routes.xml
26-
xmllint --noout --schema vendor/magento/module-config/etc/system.xsd etc/adminhtml/system.xml
26+
xmllint --noout --schema vendor/magento/module-config/etc/system_file.xsd etc/adminhtml/system.xml
2727
xmllint --noout --schema vendor/magento/framework/Acl/etc/acl.xsd etc/acl.xml
2828
xmllint --noout --schema vendor/magento/module-cron/etc/cron_groups.xsd etc/cron_groups.xml
2929
xmllint --noout --schema vendor/magento/module-cron/etc/crontab.xsd etc/crontab.xml

Storage/FileStorage.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,40 @@
66

77
use Baldwin\UrlDataIntegrityChecker\Exception\SerializationException;
88
use Magento\Framework\App\Config\ScopeConfigInterface;
9-
use Magento\Store\Model\ScopeInterface;
109
use Magento\Framework\App\Filesystem\DirectoryList;
1110
use Magento\Framework\Exception\FileSystemException;
1211
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;
1315

1416
class FileStorage extends AbstractStorage implements StorageInterface
1517
{
16-
const CONFIG_PATH = 'url/checker/path';
18+
const CONFIG_PATH = 'url_data_integrity_checker/configuration/filestorage_path';
1719

1820
private $filesystem;
1921
private $scopeConfig;
22+
private $writeFactory;
23+
private $readFactory;
24+
private $fileDriver;
2025

2126
public function __construct(
2227
Filesystem $filesystem,
23-
ScopeConfigInterface $scopeConfig
28+
ScopeConfigInterface $scopeConfig,
29+
WriteFactory $writeFactory,
30+
ReadFactory $readFactory,
31+
FileDriver $fileDriver
2432
) {
2533
$this->filesystem = $filesystem;
2634
$this->scopeConfig = $scopeConfig;
35+
$this->writeFactory = $writeFactory;
36+
$this->readFactory = $readFactory;
37+
$this->fileDriver = $fileDriver;
2738
}
2839

2940
public function write(string $identifier, array $data): bool
3041
{
31-
$directory = $this->filesystem->getDirectoryWrite($this->getPath() ?: DirectoryList::TMP);
42+
$directory = $this->writeFactory->create($this->getPath());
3243
$directory->create();
3344
$filename = $this->getFilename($identifier);
3445

@@ -46,7 +57,7 @@ public function write(string $identifier, array $data): bool
4657

4758
public function read(string $identifier): array
4859
{
49-
$directory = $this->filesystem->getDirectoryRead($this->getPath() ?: DirectoryList::TMP);
60+
$directory = $this->readFactory->create($this->getPath());
5061
$filename = $this->getFilename($identifier);
5162

5263
$data = '';
@@ -67,9 +78,31 @@ private function getFilename(string $identifier): string
6778

6879
private function getPath(): string
6980
{
70-
return $this->scopeConfig->getValue(
71-
self::CONFIG_PATH,
72-
ScopeInterface::SCOPE_STORE
81+
$path = $this->scopeConfig->getValue(
82+
self::CONFIG_PATH
7383
);
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;
74107
}
75108
}

etc/acl.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<resource id="Magento_Backend::stores">
1010
<resource id="Magento_Backend::stores_settings">
1111
<resource id="Magento_Config::config">
12-
<resource id="Baldwin_UrlDataIntegrityChecker::config_baldwin_urldataintegritychecker" title="url"/>
12+
<resource id="Baldwin_UrlDataIntegrityChecker::config_data_integrity" title="Data Integrity Checker Config"/>
1313
</resource>
1414
</resource>
1515
</resource>

etc/adminhtml/system.xml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
<?xml version="1.0" ?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
33
<system>
4-
<tab id="baldwin" translate="label" sortOrder="500">
5-
<label>Baldwin</label>
6-
</tab>
7-
<section id="url" sortOrder="10" showInWebsite="0" showInStore="0" showInDefault="1" translate="label">
4+
<section id="url_data_integrity_checker" sortOrder="200" showInDefault="1" showInWebsite="0" showInStore="0" translate="label">
85
<label>Url Data Integrity Checker</label>
9-
<tab>baldwin</tab>
10-
<resource>Baldwin_UrlDataIntegrityChecker::config_baldwin_urldataintegritychecker</resource>
11-
<group id="checker" sortOrder="10" showInWebsite="0" showInStore="0" showInDefault="1" translate="label">
12-
<label>Config</label>
13-
<field id="path" type="text" sortOrder="10" showInWebsite="0" showInStore="0" showInDefault="1" translate="label">
14-
<label>Path</label>
15-
<comment>Path where to store json files eg tmp or media</comment>
6+
<tab>catalog</tab>
7+
<resource>Baldwin_UrlDataIntegrityChecker::config_data_integrity</resource>
8+
<group id="configuration" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0" translate="label">
9+
<label>Configuration</label>
10+
<field id="filestorage_path" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0" translate="label">
11+
<label>Path to store results</label>
12+
<comment>Path where to store json files, can be either an absolute or a relative path</comment>
1613
</field>
1714
</group>
1815
</section>
1916
</system>
20-
</config>
17+
</config>

etc/config.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" ?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
33
<default>
4-
<url>
5-
<checker>
6-
<path>tmp</path>
7-
</checker>
8-
</url>
4+
<url_data_integrity_checker>
5+
<configuration>
6+
<filestorage_path>var/tmp</filestorage_path>
7+
</configuration>
8+
</url_data_integrity_checker>
99
</default>
10-
</config>
10+
</config>

0 commit comments

Comments
 (0)