Skip to content

Commit

Permalink
fix migrate service
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Jul 16, 2021
1 parent fff5bb7 commit 2aff9cf
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
10 changes: 9 additions & 1 deletion Model/Document/Filesystem/PathResolver/RestrictedResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

use Magento\Framework\Model\AbstractModel;
use Opengento\Document\Api\Data\DocumentTypeInterface;
use Opengento\Document\Model\Document\Filesystem\PathResolverInterface;

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

public function resolvePath(DocumentTypeInterface $documentType): string
{
return $documentType->getExtensionAttributes()->getIsRestricted() ? self::RESTRICT_PATH : '';
return $this->isRestricted($documentType) ? self::RESTRICT_PATH : '';
}

private function isRestricted(DocumentTypeInterface $documentType): bool
{
return (bool) ($documentType instanceof AbstractModel
? $documentType->getData('is_restricted')
: $documentType->getExtensionAttributes()->getIsRestricted());
}
}
11 changes: 10 additions & 1 deletion Model/Document/Filesystem/UrlResolver/RestrictedResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
namespace Opengento\DocumentRestrict\Model\Document\Filesystem\UrlResolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\UrlInterface;
use Opengento\Document\Api\Data\DocumentInterface;
use Opengento\Document\Api\Data\DocumentTypeInterface;
use Opengento\Document\Api\DocumentTypeRepositoryInterface;
use Opengento\Document\Model\Document\Filesystem\UrlResolverInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -50,8 +52,15 @@ public function getFileUrl(DocumentInterface $document): string
$documentType = null;
}

return $documentType && $documentType->getExtensionAttributes()->getIsRestricted()
return $documentType && $this->isRestricted($documentType)
? $this->urlBuilder->getUrl('document/restrict/view', ['id' => $document->getId()])
: '';
}

private function isRestricted(DocumentTypeInterface $documentType): bool
{
return (bool) ($documentType instanceof AbstractModel
? $documentType->getData('is_restricted')
: $documentType->getExtensionAttributes()->getIsRestricted());
}
}
28 changes: 18 additions & 10 deletions Model/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public function __construct(
*/
public function migrateQueue(): void
{
$typeIds = $this->migrateDb->fetchPendingTypeIds();
$typeIds = $this->migrateDb->fetchQueuedTypeIds();
$failedTypeIds = [];

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

// Todo: handle batch management to avoid out of memory on large dataset with tons of files
$documentCollection = $this->createDocumentCollection($typeIds);
Expand All @@ -101,15 +101,12 @@ public function migrateQueue(): void
$docTypeCollection->getItemById($document->getTypeId()),
$filePath
);
$document = $this->hydrator->hydrate(
$document,
['file_path' => dirname($destPath), 'file_name' => basename($destPath)]
);

try {
$this->file->moveFile($filePath, $destPath);
$this->documentRepository->save($document);
$this->documentRepository->save($this->updateDocument($document, $destPath));
} catch (CouldNotSaveException $e) {
$this->logger->error($e->getMessage(), $e->getTrace());
$this->logger->error($e->getPrevious()->getMessage(), $e->getTrace());
$this->file->moveFile($destPath, $filePath);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), $e->getTrace());
Expand All @@ -118,13 +115,24 @@ public function migrateQueue(): void
}

// End migration
$this->migrateDb->updateState(array_diff($typeIds, $failedTypeIds), 'completed');
$this->migrateDb->updateCompleteState(array_diff($typeIds, $failedTypeIds));
$this->migrateDb->updateFailureState($failedTypeIds);
}

private function updateDocument(DocumentInterface $document, string $filePath): DocumentInterface
{
return $this->hydrator->hydrate(
$document,
[
'file_path' => dirname($this->file->getRelativeFilePath($filePath)),
'file_name' => basename($filePath)
]
);
}

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

return $documentCollection;
Expand Down
40 changes: 34 additions & 6 deletions Model/ResourceModel/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,62 @@

class Migrate extends AbstractDb
{
public const STATE_PENDING = 'pending';
public const STATE_RUNNING = 'running';
public const STATE_COMPLETE = 'complete';
public const STATE_FAILURE = 'failure';

protected function _construct(): void
{
$this->_init('opengento_document_type_restrict_migrate', 'entity_id');
}

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

public function fetchPendingTypeIds(): array
public function fetchQueuedTypeIds(): array
{
$select = $this->getConnection()->select();
$select->from($this->getMainTable(), 'type_id')->where('state = ?', 'pending', 'VARCHAR');
$select->from($this->getMainTable(), 'type_id');
$select->where('state IN (?)', [self::STATE_PENDING, self::STATE_FAILURE]);

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

public function updateState(array $typeIds, string $state): void
public function updateRunningState(array $typeIds): void
{
$this->getConnection()->update($this->getMainTable(), ['state' => $state], ['type_id IN (?)' => $typeIds]);
$this->getConnection()->update(
$this->getMainTable(),
['state' => self::STATE_RUNNING],
['type_id IN (?)' => $typeIds, 'state IN (?)' => [self::STATE_PENDING, self::STATE_FAILURE]]
);
}

public function updateCompleteState(array $typeIds): void
{
$this->getConnection()->update(
$this->getMainTable(),
['state' => self::STATE_COMPLETE],
['type_id IN (?)' => $typeIds, 'state = ?' => self::STATE_RUNNING]
);
}

public function updateFailureState(array $typeIds): void
{
$this->getConnection()->update(
$this->getMainTable(),
['state' => self::STATE_FAILURE],
['type_id IN (?)' => $typeIds, 'state = ?' => self::STATE_RUNNING]
);
}

public function deleteOlderThan(DateTime $dateTime): void
{
$this->getConnection()->delete(
$this->getMainTable(),
['state = ?' => 'complete', 'updated_at <= ?' => $dateTime]
['state = ?' => self::STATE_COMPLETE, 'updated_at <= ?' => $dateTime]
);
}
}
14 changes: 14 additions & 0 deletions etc/directory.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_MediaGalleryApi:etc/directory.xsd">
<exclude>
<patterns>
<pattern name="opengento_document_restricted">/^downloadable/document/</pattern>
</patterns>
</exclude>
</config>

0 comments on commit 2aff9cf

Please sign in to comment.