Skip to content

Commit fff5bb7

Browse files
committed
fix migrate persistance
1 parent 6752627 commit fff5bb7

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

Cron/Migrate.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Opengento\DocumentRestrict\Cron;
99

10+
use Magento\Framework\Exception\FileSystemException;
1011
use Opengento\DocumentRestrict\Model\Migrate as MigrateService;
12+
use Psr\Log\LoggerInterface;
1113

1214
final class Migrate
1315
{
@@ -16,14 +18,25 @@ final class Migrate
1618
*/
1719
private $migrateService;
1820

21+
/**
22+
* @var LoggerInterface
23+
*/
24+
private $logger;
25+
1926
public function __construct(
20-
MigrateService $migrateService
27+
MigrateService $migrateService,
28+
LoggerInterface $logger
2129
) {
2230
$this->migrateService = $migrateService;
31+
$this->logger = $logger;
2332
}
2433

2534
public function execute(): void
2635
{
27-
$this->migrateService->migrateQueue();
36+
try {
37+
$this->migrateService->migrateQueue();
38+
} catch (FileSystemException $e) {
39+
$this->logger->error($e->getLogMessage(), $e->getTrace());
40+
}
2841
}
2942
}

Model/Document/Filesystem/UrlResolver/RestrictedResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(
4141
$this->logger = $logger;
4242
}
4343

44-
public function getUrl(DocumentInterface $document): string
44+
public function getFileUrl(DocumentInterface $document): string
4545
{
4646
try {
4747
$documentType = $this->documentTypeRepository->getById($document->getTypeId());

Model/Migrate.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
namespace Opengento\DocumentRestrict\Model;
99

1010
use Exception;
11+
use Magento\Framework\EntityManager\HydratorInterface;
12+
use Magento\Framework\EntityManager\HydratorPool;
13+
use Magento\Framework\Exception\CouldNotSaveException;
14+
use Magento\Framework\Exception\FileSystemException;
15+
use Opengento\Document\Api\Data\DocumentInterface;
16+
use Opengento\Document\Api\DocumentRepositoryInterface;
1117
use Opengento\Document\Model\Document\Filesystem\File;
1218
use Opengento\Document\Model\ResourceModel\DocumentType\Collection as DocTypeCollection;
1319
use Opengento\Document\Model\ResourceModel\DocumentType\CollectionFactory as DocTypeCollectionFactory;
@@ -16,6 +22,8 @@
1622
use Opengento\Document\Model\ResourceModel\Document\CollectionFactory as DocumentCollectionFactory;
1723
use Psr\Log\LoggerInterface;
1824
use function array_diff;
25+
use function basename;
26+
use function dirname;
1927

2028
final class Migrate
2129
{
@@ -34,6 +42,16 @@ final class Migrate
3442
*/
3543
private $docTypeCollectionFactory;
3644

45+
/**
46+
* @var DocumentRepositoryInterface
47+
*/
48+
private $documentRepository;
49+
50+
/**
51+
* @var HydratorInterface
52+
*/
53+
private $hydrator;
54+
3755
/**
3856
* @var File
3957
*/
@@ -48,16 +66,23 @@ public function __construct(
4866
MigrateDb $migrateDb,
4967
DocumentCollectionFactory $documentCollectionFactory,
5068
DocTypeCollectionFactory $docTypeCollectionFactory,
69+
DocumentRepositoryInterface $documentRepository,
70+
HydratorPool $hydratorPool,
5171
File $file,
5272
LoggerInterface $logger
5373
) {
5474
$this->migrateDb = $migrateDb;
5575
$this->documentCollectionFactory = $documentCollectionFactory;
5676
$this->docTypeCollectionFactory = $docTypeCollectionFactory;
77+
$this->documentRepository = $documentRepository;
78+
$this->hydrator = $hydratorPool->getHydrator(DocumentInterface::class);
5779
$this->file = $file;
5880
$this->logger = $logger;
5981
}
6082

83+
/**
84+
* @throws FileSystemException
85+
*/
6186
public function migrateQueue(): void
6287
{
6388
$typeIds = $this->migrateDb->fetchPendingTypeIds();
@@ -72,11 +97,20 @@ public function migrateQueue(): void
7297

7398
foreach ($documentCollection->getItems() as $document) {
7499
$filePath = $this->file->getFilePath($document);
100+
$destPath = $this->file->getFileDestPath(
101+
$docTypeCollection->getItemById($document->getTypeId()),
102+
$filePath
103+
);
104+
$document = $this->hydrator->hydrate(
105+
$document,
106+
['file_path' => dirname($destPath), 'file_name' => basename($destPath)]
107+
);
75108
try {
76-
$this->file->moveFile(
77-
$filePath,
78-
$this->file->getFileDestPath($docTypeCollection->getItemById($document->getTypeId()), $filePath)
79-
);
109+
$this->file->moveFile($filePath, $destPath);
110+
$this->documentRepository->save($document);
111+
} catch (CouldNotSaveException $e) {
112+
$this->logger->error($e->getMessage(), $e->getTrace());
113+
$this->file->moveFile($destPath, $filePath);
80114
} catch (Exception $e) {
81115
$this->logger->error($e->getMessage(), $e->getTrace());
82116
$failedTypeIds[$document->getTypeId()] = $document->getTypeId();

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
"type": "magento2-module",
1616
"require": {
1717
"magento/framework": "^103.0",
18-
"opengento/module-document": "^1.0"
18+
"opengento/module-document": "^2.0"
1919
},
2020
"require-dev": {
2121
"magento/magento-coding-standard": "^5",
2222
"magento/marketplace-eqp": "^4.0",
23-
"roave/security-advisories": "dev-latest"
23+
"roave/security-advisories": "dev-master"
2424
},
2525
"suggest": {
2626
"opengento/module-document-product-link": "This module aims to help merchants to link their documents to products in Magento 2.",

etc/db_schema.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
<index referenceId="OPENGENTO_DOCUMENT_TYPE_RESTRICT_MIGRATE_TYPE_ID" indexType="btree">
4545
<column name="type_id"/>
4646
</index>
47+
<index referenceId="OPENGENTO_DOCUMENT_TYPE_RESTRICT_MIGRATE_STATE" indexType="btree">
48+
<column name="state"/>
49+
</index>
4750
</table>
4851
<!-- opengento_document_type_restrict_customer -->
4952
<!-- new relations in (document type or document) -->

etc/di.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@
5353
</type>
5454
<type name="Opengento\Document\Model\Document\Filesystem\UrlResolver">
5555
<arguments>
56-
<argument name="urlResolvers" xsi:type="array">
57-
<item name="restricted" xsi:type="object">Opengento\DocumentRestrict\Model\Document\Filesystem\UrlResolver\RestrictedResolver</item>
56+
<argument name="resolvers" xsi:type="array">
57+
<item name="restricted" xsi:type="array">
58+
<item name="sortOrder" xsi:type="number">10</item>
59+
<item name="resolver" xsi:type="object">Opengento\DocumentRestrict\Model\Document\Filesystem\UrlResolver\RestrictedResolver</item>
60+
</item>
5861
</argument>
5962
</arguments>
6063
</type>

0 commit comments

Comments
 (0)