Skip to content

Commit c378f41

Browse files
committed
Add restrict migration
1 parent 8a65e29 commit c378f41

File tree

16 files changed

+308
-25
lines changed

16 files changed

+308
-25
lines changed

Controller/Restrict/View.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function execute()
8484
$this->logger->error($e->getLogMessage(), $e->getTrace());
8585
$response = $this->forwardNoRoute();
8686
} catch (EmptyAuthException $e) {
87+
$this->messageManager->addErrorMessage(new Phrase('Please authenticate first.'));
8788
$response = $this->redirectFailAuth();
8889
} catch (InvalidAuthException $e) {
8990
$this->messageManager->addErrorMessage($e->getMessage());

Cron/Migrate.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\DocumentRestrict\Cron;
9+
10+
use Opengento\DocumentRestrict\Model\Migrate as MigrateService;
11+
12+
final class Migrate
13+
{
14+
/**
15+
* @var MigrateService
16+
*/
17+
private $migrateService;
18+
19+
public function __construct(
20+
MigrateService $migrateService
21+
) {
22+
$this->migrateService = $migrateService;
23+
}
24+
25+
public function execute(): void
26+
{
27+
$this->migrateService->migrateQueue();
28+
}
29+
}

Cron/Rotate.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\DocumentRestrict\Cron;
9+
10+
use DateTime;
11+
use Exception;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Opengento\DocumentRestrict\Model\ResourceModel\Migrate as MigrateDb;
14+
use function sprintf;
15+
16+
final class Rotate
17+
{
18+
private const CONFIG_PATH_MIGRATE_DAYS_ROTATE = 'opengento_document/migrate/days_rotate';
19+
20+
/**
21+
* @var ScopeConfigInterface
22+
*/
23+
private $scopeConfig;
24+
25+
/**
26+
* @var MigrateDb
27+
*/
28+
private $migrateDb;
29+
30+
public function __construct(
31+
ScopeConfigInterface $scopeConfig,
32+
MigrateDb $migrateDb
33+
) {
34+
$this->scopeConfig = $scopeConfig;
35+
$this->migrateDb = $migrateDb;
36+
}
37+
38+
/**
39+
* @throws Exception
40+
*/
41+
public function execute(): void
42+
{
43+
$this->migrateDb->deleteOlderThan(
44+
new DateTime(sprintf('-%sday', $this->scopeConfig->getValue(self::CONFIG_PATH_MIGRATE_DAYS_ROTATE)))
45+
);
46+
}
47+
}

Model/Document/Filesystem/UrlResolver/RestrictedResolver.php

Lines changed: 2 additions & 2 deletions
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 getUrl(DocumentInterface $document): string
4545
{
4646
try {
4747
$documentType = $this->documentTypeRepository->getById($document->getTypeId());
@@ -52,6 +52,6 @@ public function getUrl(DocumentInterface $document): ?string
5252

5353
return $documentType && $documentType->getExtensionAttributes()->getIsRestricted()
5454
? $this->urlBuilder->getUrl('document/restrict/view', ['id' => $document->getId()])
55-
: null;
55+
: '';
5656
}
5757
}

Model/Migrate.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\DocumentRestrict\Model;
9+
10+
use Exception;
11+
use Opengento\Document\Model\Document\Filesystem\File;
12+
use Opengento\Document\Model\ResourceModel\DocumentType\Collection as DocTypeCollection;
13+
use Opengento\Document\Model\ResourceModel\DocumentType\CollectionFactory as DocTypeCollectionFactory;
14+
use Opengento\DocumentRestrict\Model\ResourceModel\Migrate as MigrateDb;
15+
use Opengento\Document\Model\ResourceModel\Document\Collection as DocumentCollection;
16+
use Opengento\Document\Model\ResourceModel\Document\CollectionFactory as DocumentCollectionFactory;
17+
use Psr\Log\LoggerInterface;
18+
use function array_diff;
19+
20+
final class Migrate
21+
{
22+
/**
23+
* @var MigrateDb
24+
*/
25+
private $migrateDb;
26+
27+
/**
28+
* @var DocumentCollectionFactory
29+
*/
30+
private $documentCollectionFactory;
31+
32+
/**
33+
* @var DocTypeCollectionFactory
34+
*/
35+
private $docTypeCollectionFactory;
36+
37+
/**
38+
* @var File
39+
*/
40+
private $file;
41+
42+
/**
43+
* @var LoggerInterface
44+
*/
45+
private $logger;
46+
47+
public function __construct(
48+
MigrateDb $migrateDb,
49+
DocumentCollectionFactory $documentCollectionFactory,
50+
DocTypeCollectionFactory $docTypeCollectionFactory,
51+
File $file,
52+
LoggerInterface $logger
53+
) {
54+
$this->migrateDb = $migrateDb;
55+
$this->documentCollectionFactory = $documentCollectionFactory;
56+
$this->docTypeCollectionFactory = $docTypeCollectionFactory;
57+
$this->file = $file;
58+
$this->logger = $logger;
59+
}
60+
61+
public function migrateQueue(): void
62+
{
63+
$typeIds = $this->migrateDb->fetchPendingTypeIds();
64+
$failedTypeIds = [];
65+
66+
// Start migration
67+
$this->migrateDb->updateState($typeIds, 'running');
68+
69+
$documentCollection = $this->createDocumentCollection($typeIds);
70+
$docTypeCollection = $this->createDocTypeCollection($typeIds);
71+
72+
foreach ($documentCollection->getItems() as $document) {
73+
$filePath = $this->file->getFilePath($document);
74+
try {
75+
$this->file->moveFile(
76+
$filePath,
77+
$this->file->getFileDestPath($docTypeCollection->getItemById($document->getTypeId()), $filePath)
78+
);
79+
} catch (Exception $e) {
80+
$this->logger->error($e->getMessage(), $e->getTrace());
81+
$failedTypeIds[$document->getTypeId()] = $document->getTypeId();
82+
}
83+
}
84+
85+
// End migration
86+
$this->migrateDb->updateState(array_diff($typeIds, $failedTypeIds), 'completed');
87+
}
88+
89+
private function createDocumentCollection(array $typeIds): DocumentCollection
90+
{
91+
$documentCollection = $this->documentCollectionFactory->create();
92+
$documentCollection->addFieldToSelect(['type_id', 'file_name', 'file_path']);
93+
$documentCollection->addFieldToFilter('type_id', ['in' => $typeIds]);
94+
95+
return $documentCollection;
96+
}
97+
98+
private function createDocTypeCollection(array $typeIds): DocTypeCollection
99+
{
100+
$docTypeCollection = $this->docTypeCollectionFactory->create();
101+
$docTypeCollection->addFieldToSelect(['entity_id', 'file_dest_path', 'sub_path_length', 'is_restricted']);
102+
$docTypeCollection->addFieldToFilter('type_id', ['in' => $typeIds]);
103+
104+
return $docTypeCollection;
105+
}
106+
}

Model/ResourceModel/DocumentType/Relation/Restrict.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@
1010
use Magento\Framework\Model\AbstractModel;
1111
use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationInterface;
1212
use Opengento\Document\Api\Data\DocumentTypeInterface;
13+
use Opengento\DocumentRestrict\Model\ResourceModel\Migrate;
1314

1415
final class Restrict implements RelationInterface
1516
{
17+
/**
18+
* @var Migrate
19+
*/
20+
private $migrate;
21+
22+
public function __construct(
23+
Migrate $migrate
24+
) {
25+
$this->migrate = $migrate;
26+
}
27+
1628
public function processRelation(AbstractModel $object): void
1729
{
18-
if ($object instanceof DocumentTypeInterface) {
19-
$origIsRestricted = $object->getOrigData('is_restricted');
20-
$isRestricted = $object->getExtensionAttributes()->getIsRestricted();
21-
22-
if ($origIsRestricted !== $isRestricted) {
23-
if ($origIsRestricted) {
24-
//todo Move private to public: use batch action; DocumentType visibility shouldn't be updated till all is moved
25-
} elseif ($isRestricted) {
26-
//todo Move public to private: use batch action; DocumentType visibility shouldn't be updated till all is moved
27-
}
28-
}
30+
if (
31+
$object instanceof DocumentTypeInterface &&
32+
$object->getOrigData('is_restricted') !== $object->getData('is_restricted')
33+
) {
34+
$this->migrate->scheduleMigration((int) $object->getId());
2935
}
3036
}
3137
}

Model/ResourceModel/Migrate.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\DocumentRestrict\Model\ResourceModel;
9+
10+
use DateTime;
11+
use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
12+
13+
class Migrate extends AbstractDb
14+
{
15+
protected function _construct(): void
16+
{
17+
$this->_init('opengento_document_type_restrict_migrate', 'entity_id');
18+
}
19+
20+
public function scheduleMigration(int $typeId): void
21+
{
22+
$this->getConnection()->insert($this->getMainTable(), ['state' => 'pending', 'type_id' => $typeId]);
23+
}
24+
25+
public function fetchPendingTypeIds(): array
26+
{
27+
$select = $this->getConnection()->select();
28+
$select->from($this->getMainTable(), 'type_id')->where('state = ?', 'pending', 'VARCHAR');
29+
30+
return $this->getConnection()->fetchCol($select);
31+
}
32+
33+
public function updateState(array $typeIds, string $state): void
34+
{
35+
$this->getConnection()->update($this->getMainTable(), ['state' => $state], ['type_id IN (?)' => $typeIds]);
36+
}
37+
38+
public function deleteOlderThan(DateTime $dateTime): void
39+
{
40+
$this->getConnection()->delete(
41+
$this->getMainTable(),
42+
['state = ?' => 'complete', 'updated_at <= ?' => $dateTime]
43+
);
44+
}
45+
}

Model/ResourceModel/Restrict.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111

1212
class Restrict extends AbstractDb
1313
{
14-
/**
15-
* @inheritdoc
16-
*/
17-
protected function _construct()
14+
protected function _construct(): void
1815
{
1916
$this->_init('opengento_document_type_restrict', 'entity_id');
2017
}

Model/ResourceModel/Restrict/Collection.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
class Collection extends AbstractCollection
1515
{
16-
/**
17-
* @inheritdoc
18-
*/
19-
protected function _construct()
16+
protected function _construct(): void
2017
{
2118
$this->_init(Model::class, ResourceModel::class);
2219
}

Model/Restrict.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Restrict extends AbstractExtensibleModel implements RestrictInterface, Ide
2020
{
2121
public const CACHE_TAG = 'ope_dtr';
2222

23-
protected function _construct()
23+
protected function _construct(): void
2424
{
2525
$this->_init(ResourceModel::class);
2626
}

0 commit comments

Comments
 (0)