Skip to content

Commit 2aff9cf

Browse files
committed
fix migrate service
1 parent fff5bb7 commit 2aff9cf

File tree

5 files changed

+85
-18
lines changed

5 files changed

+85
-18
lines changed

Model/Document/Filesystem/PathResolver/RestrictedResolver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Opengento\DocumentRestrict\Model\Document\Filesystem\PathResolver;
99

10+
use Magento\Framework\Model\AbstractModel;
1011
use Opengento\Document\Api\Data\DocumentTypeInterface;
1112
use Opengento\Document\Model\Document\Filesystem\PathResolverInterface;
1213

@@ -16,6 +17,13 @@ final class RestrictedResolver implements PathResolverInterface
1617

1718
public function resolvePath(DocumentTypeInterface $documentType): string
1819
{
19-
return $documentType->getExtensionAttributes()->getIsRestricted() ? self::RESTRICT_PATH : '';
20+
return $this->isRestricted($documentType) ? self::RESTRICT_PATH : '';
21+
}
22+
23+
private function isRestricted(DocumentTypeInterface $documentType): bool
24+
{
25+
return (bool) ($documentType instanceof AbstractModel
26+
? $documentType->getData('is_restricted')
27+
: $documentType->getExtensionAttributes()->getIsRestricted());
2028
}
2129
}

Model/Document/Filesystem/UrlResolver/RestrictedResolver.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
namespace Opengento\DocumentRestrict\Model\Document\Filesystem\UrlResolver;
99

1010
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Model\AbstractModel;
1112
use Magento\Framework\UrlInterface;
1213
use Opengento\Document\Api\Data\DocumentInterface;
14+
use Opengento\Document\Api\Data\DocumentTypeInterface;
1315
use Opengento\Document\Api\DocumentTypeRepositoryInterface;
1416
use Opengento\Document\Model\Document\Filesystem\UrlResolverInterface;
1517
use Psr\Log\LoggerInterface;
@@ -50,8 +52,15 @@ public function getFileUrl(DocumentInterface $document): string
5052
$documentType = null;
5153
}
5254

53-
return $documentType && $documentType->getExtensionAttributes()->getIsRestricted()
55+
return $documentType && $this->isRestricted($documentType)
5456
? $this->urlBuilder->getUrl('document/restrict/view', ['id' => $document->getId()])
5557
: '';
5658
}
59+
60+
private function isRestricted(DocumentTypeInterface $documentType): bool
61+
{
62+
return (bool) ($documentType instanceof AbstractModel
63+
? $documentType->getData('is_restricted')
64+
: $documentType->getExtensionAttributes()->getIsRestricted());
65+
}
5766
}

Model/Migrate.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ public function __construct(
8585
*/
8686
public function migrateQueue(): void
8787
{
88-
$typeIds = $this->migrateDb->fetchPendingTypeIds();
88+
$typeIds = $this->migrateDb->fetchQueuedTypeIds();
8989
$failedTypeIds = [];
9090

9191
// Start migration
92-
$this->migrateDb->updateState($typeIds, 'running');
92+
$this->migrateDb->updateRunningState($typeIds);
9393

9494
// Todo: handle batch management to avoid out of memory on large dataset with tons of files
9595
$documentCollection = $this->createDocumentCollection($typeIds);
@@ -101,15 +101,12 @@ public function migrateQueue(): void
101101
$docTypeCollection->getItemById($document->getTypeId()),
102102
$filePath
103103
);
104-
$document = $this->hydrator->hydrate(
105-
$document,
106-
['file_path' => dirname($destPath), 'file_name' => basename($destPath)]
107-
);
104+
108105
try {
109106
$this->file->moveFile($filePath, $destPath);
110-
$this->documentRepository->save($document);
107+
$this->documentRepository->save($this->updateDocument($document, $destPath));
111108
} catch (CouldNotSaveException $e) {
112-
$this->logger->error($e->getMessage(), $e->getTrace());
109+
$this->logger->error($e->getPrevious()->getMessage(), $e->getTrace());
113110
$this->file->moveFile($destPath, $filePath);
114111
} catch (Exception $e) {
115112
$this->logger->error($e->getMessage(), $e->getTrace());
@@ -118,13 +115,24 @@ public function migrateQueue(): void
118115
}
119116

120117
// End migration
121-
$this->migrateDb->updateState(array_diff($typeIds, $failedTypeIds), 'completed');
118+
$this->migrateDb->updateCompleteState(array_diff($typeIds, $failedTypeIds));
119+
$this->migrateDb->updateFailureState($failedTypeIds);
120+
}
121+
122+
private function updateDocument(DocumentInterface $document, string $filePath): DocumentInterface
123+
{
124+
return $this->hydrator->hydrate(
125+
$document,
126+
[
127+
'file_path' => dirname($this->file->getRelativeFilePath($filePath)),
128+
'file_name' => basename($filePath)
129+
]
130+
);
122131
}
123132

124133
private function createDocumentCollection(array $typeIds): DocumentCollection
125134
{
126135
$documentCollection = $this->documentCollectionFactory->create();
127-
$documentCollection->addFieldToSelect(['type_id', 'file_name', 'file_path']);
128136
$documentCollection->addFieldToFilter('type_id', ['in' => $typeIds]);
129137

130138
return $documentCollection;

Model/ResourceModel/Migrate.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,62 @@
1212

1313
class Migrate extends AbstractDb
1414
{
15+
public const STATE_PENDING = 'pending';
16+
public const STATE_RUNNING = 'running';
17+
public const STATE_COMPLETE = 'complete';
18+
public const STATE_FAILURE = 'failure';
19+
1520
protected function _construct(): void
1621
{
1722
$this->_init('opengento_document_type_restrict_migrate', 'entity_id');
1823
}
1924

2025
public function scheduleMigration(int $typeId): void
2126
{
22-
$this->getConnection()->insert($this->getMainTable(), ['state' => 'pending', 'type_id' => $typeId]);
27+
$this->getConnection()->insert($this->getMainTable(), ['state' => self::STATE_PENDING, 'type_id' => $typeId]);
2328
}
2429

25-
public function fetchPendingTypeIds(): array
30+
public function fetchQueuedTypeIds(): array
2631
{
2732
$select = $this->getConnection()->select();
28-
$select->from($this->getMainTable(), 'type_id')->where('state = ?', 'pending', 'VARCHAR');
33+
$select->from($this->getMainTable(), 'type_id');
34+
$select->where('state IN (?)', [self::STATE_PENDING, self::STATE_FAILURE]);
2935

3036
return $this->getConnection()->fetchCol($select);
3137
}
3238

33-
public function updateState(array $typeIds, string $state): void
39+
public function updateRunningState(array $typeIds): void
3440
{
35-
$this->getConnection()->update($this->getMainTable(), ['state' => $state], ['type_id IN (?)' => $typeIds]);
41+
$this->getConnection()->update(
42+
$this->getMainTable(),
43+
['state' => self::STATE_RUNNING],
44+
['type_id IN (?)' => $typeIds, 'state IN (?)' => [self::STATE_PENDING, self::STATE_FAILURE]]
45+
);
46+
}
47+
48+
public function updateCompleteState(array $typeIds): void
49+
{
50+
$this->getConnection()->update(
51+
$this->getMainTable(),
52+
['state' => self::STATE_COMPLETE],
53+
['type_id IN (?)' => $typeIds, 'state = ?' => self::STATE_RUNNING]
54+
);
55+
}
56+
57+
public function updateFailureState(array $typeIds): void
58+
{
59+
$this->getConnection()->update(
60+
$this->getMainTable(),
61+
['state' => self::STATE_FAILURE],
62+
['type_id IN (?)' => $typeIds, 'state = ?' => self::STATE_RUNNING]
63+
);
3664
}
3765

3866
public function deleteOlderThan(DateTime $dateTime): void
3967
{
4068
$this->getConnection()->delete(
4169
$this->getMainTable(),
42-
['state = ?' => 'complete', 'updated_at <= ?' => $dateTime]
70+
['state = ?' => self::STATE_COMPLETE, 'updated_at <= ?' => $dateTime]
4371
);
4472
}
4573
}

etc/directory.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © OpenGento, All rights reserved.
5+
* See LICENSE bundled with this library for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_MediaGalleryApi:etc/directory.xsd">
9+
<exclude>
10+
<patterns>
11+
<pattern name="opengento_document_restricted">/^downloadable/document/</pattern>
12+
</patterns>
13+
</exclude>
14+
</config>

0 commit comments

Comments
 (0)