Skip to content

Commit 21180c0

Browse files
Save relative path in xml feed file path declaration
1 parent 0212bd9 commit 21180c0

File tree

7 files changed

+336
-48
lines changed

7 files changed

+336
-48
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright © Magmodules.eu. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magmodules\Sooqr\Controller\Adminhtml\Feed;
9+
10+
use Magento\Backend\App\Action;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Ui\Component\MassAction\Filter;
13+
use Magento\Framework\Controller\Result\Redirect;
14+
use Magento\Framework\Controller\ResultFactory;
15+
use Magmodules\Sooqr\Api\Feed\RepositoryInterface as FeedRepository;
16+
use Magmodules\Sooqr\Model\Feed\CollectionFactory;
17+
use Magmodules\Sooqr\Model\Feed\Collection as FeedCollection;
18+
19+
/**
20+
* Mass Delete Feed Controller
21+
*/
22+
class MassDelete extends Action
23+
{
24+
public const ADMIN_RESOURCE = 'Magmodules_Sooqr::feed';
25+
26+
private FeedRepository $feedRepository;
27+
private CollectionFactory $collectionFactory;
28+
private Filter $filter;
29+
30+
/**
31+
* Constructor.
32+
*
33+
* @param Action\Context $context
34+
* @param Filter $filter
35+
* @param CollectionFactory $collectionFactory
36+
* @param FeedRepository $feedRepository
37+
*/
38+
public function __construct(
39+
Action\Context $context,
40+
Filter $filter,
41+
CollectionFactory $collectionFactory,
42+
FeedRepository $feedRepository
43+
) {
44+
parent::__construct($context);
45+
$this->filter = $filter;
46+
$this->collectionFactory = $collectionFactory;
47+
$this->feedRepository = $feedRepository;
48+
}
49+
50+
/**
51+
* Execute function to mass delete Google Shopping feeds in admin.
52+
*
53+
* @return Redirect
54+
* @throws LocalizedException
55+
*/
56+
public function execute(): Redirect
57+
{
58+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
59+
60+
$deletedCount = 0;
61+
foreach ($this->getCollection() as $feed) {
62+
try {
63+
$this->feedRepository->deleteById((int)$feed->getId());
64+
$deletedCount++;
65+
} catch (\Exception $e) {
66+
$this->messageManager->addErrorMessage(
67+
__('Error deleting feed ID %1: %2', $feed->getId(), $e->getMessage())
68+
);
69+
}
70+
}
71+
72+
if ($deletedCount > 0) {
73+
$this->messageManager->addSuccessMessage(__('Successfully deleted %1 feed(s).', $deletedCount));
74+
}
75+
76+
return $resultRedirect->setPath('*/*/');
77+
}
78+
79+
/**
80+
* Get selected collection
81+
*
82+
* @return FeedCollection $collection
83+
* @throws LocalizedException
84+
*/
85+
private function getCollection(): FeedCollection
86+
{
87+
if ($selected = $this->getRequest()->getParam('selected')) {
88+
$collection = $this->collectionFactory->create()
89+
->addFieldToFilter(
90+
'entity_id',
91+
['in' => $selected]
92+
);
93+
} else {
94+
/** @var FeedCollection $collection */
95+
$collection = $this->collectionFactory->create();
96+
}
97+
98+
return $collection;
99+
}
100+
}

Cron/Cleanup.php

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magmodules\Sooqr\Cron;
99

10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
1012
use Magento\Framework\App\ResourceConnection;
1113
use Magento\Framework\Exception\FileSystemException;
1214
use Magento\Framework\Filesystem\Driver\File;
@@ -19,39 +21,31 @@
1921
class Cleanup
2022
{
2123

22-
/**
23-
* @var ConfigProvider
24-
*/
25-
private $configProvider;
26-
/**
27-
* @var File
28-
*/
29-
private $file;
30-
/**
31-
* @var ResourceConnection
32-
*/
33-
private $resourceConnection;
34-
/**
35-
* @var LogRepository
36-
*/
37-
private $logger;
24+
private ConfigProvider $configProvider;
25+
private File $file;
26+
private ResourceConnection $resourceConnection;
27+
private LogRepository $logger;
28+
private DirectoryList $directoryList;
3829

3930
/**
4031
* @param ConfigProvider $configProvider
4132
* @param File $file
4233
* @param LogRepository $logger
4334
* @param ResourceConnection $resourceConnection
35+
* @param DirectoryList $directoryList
4436
*/
4537
public function __construct(
4638
ConfigProvider $configProvider,
4739
File $file,
4840
LogRepository $logger,
49-
ResourceConnection $resourceConnection
41+
ResourceConnection $resourceConnection,
42+
DirectoryList $directoryList
5043
) {
5144
$this->configProvider = $configProvider;
5245
$this->file = $file;
5346
$this->logger = $logger;
5447
$this->resourceConnection = $resourceConnection;
48+
$this->directoryList = $directoryList;
5549
}
5650

5751
/**
@@ -74,33 +68,77 @@ public function execute()
7468
private function removeFiles($offset)
7569
{
7670
$connection = $this->resourceConnection->getConnection();
77-
$selectFiles = $connection->select()->from(
78-
$this->resourceConnection->getTableName('sooqr_feed'),
79-
['filename']
80-
)->where(
81-
'created_at < ?',
82-
date("Y-m-d h:i:s", strtotime("-{$offset} days"))
83-
)->where(
84-
'filename IS NOT NULL'
85-
);
71+
$filesToDelete = $this->getFilesToDelete($connection, $offset);
8672

87-
foreach ($connection->fetchCol($selectFiles) as $filename) {
88-
try {
89-
if ($this->file->isExists($filename)) {
90-
$this->file->deleteFile($filename);
91-
}
92-
} catch (FileSystemException $exception) {
93-
$this->logger->addDebugLog('removeFiles', $exception->getMessage());
94-
}
73+
$path = $this->directoryList->getPath(DirectoryList::MEDIA) . '/sooqr/data/';
74+
75+
foreach ($filesToDelete as $filename) {
76+
$this->deleteFile($path, $filename);
9577
}
9678

79+
$this->clearFileReferencesInDatabase($connection, $offset);
80+
9781
$connection->update(
9882
$this->resourceConnection->getTableName('sooqr_feed'),
9983
['filename' => null, 'webhook_url' => null],
10084
['created_at < ?' => date("Y-m-d h:i:s", strtotime("-{$offset} days"))]
10185
);
10286
}
10387

88+
/**
89+
* Clear file references in the database for outdated entries.
90+
*
91+
* @param AdapterInterface $connection
92+
* @param int $offset
93+
* @return void
94+
*/
95+
private function clearFileReferencesInDatabase(AdapterInterface $connection, int $offset): void
96+
{
97+
$connection->update(
98+
$this->resourceConnection->getTableName('sooqr_feed'),
99+
['filename' => null],
100+
['created_at < ?' => $this->getPastDate($offset)]
101+
);
102+
}
103+
104+
/**
105+
* Delete a file and log any errors.
106+
*
107+
* @param string $path
108+
* @param string $filename
109+
* @return void
110+
*/
111+
private function deleteFile(string $path, string $filename): void
112+
{
113+
try {
114+
$filename = $path . $filename . '.xml';
115+
if ($this->file->isExists($filename)) {
116+
$this->file->deleteFile($filename);
117+
}
118+
} catch (FileSystemException $exception) {
119+
$this->logger->addDebugLog('cleanupFiles', $exception->getMessage());
120+
}
121+
}
122+
123+
/**
124+
* Get files to delete based on the offset.
125+
*
126+
* @param AdapterInterface $connection
127+
* @param int $offset
128+
* @return array
129+
*/
130+
private function getFilesToDelete(AdapterInterface $connection, int $offset): array
131+
{
132+
$tableName = $this->resourceConnection->getTableName('sooqr_feed');
133+
134+
return $connection->fetchCol(
135+
$connection->select()
136+
->from($tableName, ['filename'])
137+
->where('created_at < ?', $this->getPastDate($offset))
138+
->where('filename IS NOT NULL')
139+
);
140+
}
141+
104142
/**
105143
* Remove database entries from 'sooqr_feed' table older than offset + 14-days
106144
*
@@ -115,4 +153,15 @@ private function removeEntries($offset)
115153
['created_at < ?' => date("Y-m-d h:i:s", strtotime("-{$offset} days"))]
116154
);
117155
}
156+
157+
/**
158+
* Get a formatted past date based on the offset.
159+
*
160+
* @param int $offset
161+
* @return string
162+
*/
163+
private function getPastDate(int $offset): string
164+
{
165+
return date('Y-m-d H:i:s', strtotime("-{$offset} days"));
166+
}
118167
}

Model/Feed/DataModel.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\Api\DataObjectHelper;
1111
use Magento\Framework\Api\ExtensibleDataInterface;
12+
use Magento\Framework\App\Filesystem\DirectoryList;
1213
use Magento\Framework\Model\AbstractModel;
1314
use Magento\Framework\Model\Context;
1415
use Magento\Framework\Registry;
@@ -23,19 +24,10 @@
2324
class DataModel extends AbstractModel implements ExtensibleDataInterface, DataInterface
2425
{
2526

26-
/**
27-
* @var string
28-
*/
2927
protected $_eventPrefix = 'sooqr_feed';
30-
31-
/**
32-
* @var DataObjectHelper
33-
*/
34-
protected $dataObjectHelper;
35-
/**
36-
* @var DataInterfaceFactory
37-
*/
38-
protected $dataFactory;
28+
protected DataObjectHelper $dataObjectHelper;
29+
protected DataInterfaceFactory $dataFactory;
30+
private DirectoryList $directoryList;
3931

4032
/**
4133
* DataModel constructor.
@@ -45,6 +37,7 @@ class DataModel extends AbstractModel implements ExtensibleDataInterface, DataIn
4537
* @param DataObjectHelper $dataObjectHelper
4638
* @param ResourceModel $resource
4739
* @param Collection $collection
40+
* @param DirectoryList $directoryList
4841
* @param array $data
4942
*/
5043
public function __construct(
@@ -54,10 +47,12 @@ public function __construct(
5447
DataObjectHelper $dataObjectHelper,
5548
ResourceModel $resource,
5649
Collection $collection,
50+
DirectoryList $directoryList,
5751
array $data = []
5852
) {
5953
$this->dataFactory = $dataFactory;
6054
$this->dataObjectHelper = $dataObjectHelper;
55+
$this->directoryList = $directoryList;
6156
parent::__construct($context, $registry, $resource, $collection, $data);
6257
}
6358

@@ -196,7 +191,14 @@ public function setDownloadAt(string $downloadAt): DataInterface
196191
*/
197192
public function getFilename(): ?string
198193
{
199-
return $this->getData(self::FILENAME);
194+
if ($filename = $this->getData(self::FILENAME)) {
195+
return sprintf(
196+
'%s/sooqr/data/%s.xml',
197+
$this->directoryList->getPath(DirectoryList::MEDIA),
198+
$filename
199+
);
200+
}
201+
return null;
200202
}
201203

202204
/**

0 commit comments

Comments
 (0)