Skip to content

Commit d87ebab

Browse files
author
markus-moser
committed
[CustomerNamingScheme] add cleanup job for empty directories
1 parent 2ae8108 commit d87ebab

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/CustomerProvider/ObjectNamingScheme/DefaultObjectNamingScheme.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111

1212
namespace CustomerManagementFrameworkBundle\CustomerProvider\ObjectNamingScheme;
1313

14+
use CustomerManagementFrameworkBundle\Config;
1415
use CustomerManagementFrameworkBundle\Helper\Objects;
1516
use CustomerManagementFrameworkBundle\Model\CustomerInterface;
17+
use CustomerManagementFrameworkBundle\Traits\LoggerAware;
18+
use Pimcore\Model\Object\Folder;
19+
use Pimcore\Model\Object\Listing;
1620
use Pimcore\Model\Object\Service;
1721

1822
class DefaultObjectNamingScheme implements ObjectNamingSchemeInterface
1923
{
24+
use LoggerAware;
25+
2026
/**
2127
* @param CustomerInterface $customer
2228
* @param string $parentPath
@@ -46,6 +52,39 @@ public function apply(CustomerInterface $customer, $parentPath, $namingScheme)
4652
Objects::checkObjectKey($customer);
4753
}
4854

55+
public function cleanupEmptyFolders()
56+
{
57+
$config = Config::getConfig()->CustomerProvider;
58+
if(!$config->parentPath) {
59+
return;
60+
}
61+
62+
if(!$config->namingScheme) {
63+
return;
64+
}
65+
66+
$folders = new Listing;
67+
68+
// apply it for folders older then 10 minutes only
69+
$timestamp = time() - 60*10;
70+
71+
$folders->setCondition(
72+
"o_id in (select o_id from (select o_id, o_path, o_key, o_type, (select count(*) from objects where o_parentId = o.o_id) as counter from objects o) as temp where counter=0 and o_type = 'folder' and o_path like ? and o_creationDate < ?)",
73+
[
74+
str_replace('//', '/', $config->parentPath.'/%'),
75+
$timestamp
76+
]
77+
);
78+
79+
foreach($folders as $folder) {
80+
if($folder instanceof Folder) {
81+
$folder->delete();
82+
$this->getLogger()->error("delete empty folder ". (string) $folder);
83+
}
84+
}
85+
}
86+
87+
4988
private function correctPath($path)
5089
{
5190
return str_replace('//', '/', $path);

src/CustomerProvider/ObjectNamingScheme/ObjectNamingSchemeInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ interface ObjectNamingSchemeInterface
2323
* @return void
2424
*/
2525
public function apply(CustomerInterface $customer, $parentPath, $namingScheme);
26+
27+
/**
28+
* deletes empty subfolders of the customers folder
29+
*
30+
* @return void
31+
*/
32+
public function cleanupEmptyFolders();
2633
}

src/Event/MaintenanceEventListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ public function onMaintenance(\Pimcore\Event\System\MaintenanceEvent $e)
1717
{
1818
\Pimcore::getContainer()->get('cmf.segment_manager')->executeSegmentBuilderMaintenance();
1919
\Pimcore::getContainer()->get('cmf.customer_exporter_manager')->cleanupExportTmpData();
20+
\Pimcore::getContainer()->get('cmf.customer_provider.object_naming_scheme')->cleanupEmptyFolders();
2021
}
2122
}

src/Resources/config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ services:
100100
cmf.customer_provider.object_naming_scheme:
101101
class: CustomerManagementFrameworkBundle\CustomerProvider\ObjectNamingScheme\DefaultObjectNamingScheme
102102
lazy: true
103+
calls:
104+
- [setLogger, ['@cmf.logger']]
103105

104106
cmf.action_trigger.queue:
105107
class: CustomerManagementFrameworkBundle\ActionTrigger\Queue\DefaultQueue

0 commit comments

Comments
 (0)