Skip to content

Commit 45946f1

Browse files
committed
Add symfony command to remove expired anonymous wishlists
To reduce database table size generated by guest customers
1 parent 911dbb3 commit 45946f1

File tree

11 files changed

+221
-0
lines changed

11 files changed

+221
-0
lines changed

doc/01-installation.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,14 @@ framework:
111111
```
112112

113113
All commands from the plugin implement the `WishlistSyncCommandInterface` interface, so there is no need for other configuration.
114+
115+
## Removing anonymous wishlists after expiration period
116+
117+
You can remove anonymous wishlists that have not been updated for a specified period of time. To do so, you need to add `bitbag:remove-anonymous-wishlists` Symfony console command to your cron jobs.
118+
119+
You can specify the expiration period in your parameters file to override the default value of 30 days:
120+
121+
```yaml
122+
parameters:
123+
bitbag_sylius_wishlist_plugin.parameters.anonymous_wishlist_expiration_period: 30 days # Remove all anonymous wishlists that were updated more than 30 days ago.
124+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on [email protected].
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusWishlistPlugin\Console;
13+
14+
use BitBag\SyliusWishlistPlugin\Remover\AnonymousWishlistsRemoverInterface;
15+
use SyliusLabs\Polyfill\Symfony\FrameworkBundle\Command\ContainerAwareCommand;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
19+
/**
20+
* @final
21+
*/
22+
class RemoveAnonymousWishlistsCommand extends ContainerAwareCommand
23+
{
24+
protected static $defaultName = 'bitbag:remove-anonymous-wishlists';
25+
26+
protected function configure(): void
27+
{
28+
$this
29+
->setDescription('Removes anonymous wishlists that have been idle for a period set in `bitbag_sylius_wishlist_plugin.parameters.anonymous_wishlist_expiration_period` configuration key.')
30+
;
31+
}
32+
33+
protected function execute(InputInterface $input, OutputInterface $output)
34+
{
35+
/** @var string $expirationTime */
36+
$expirationTime = $this->getContainer()->getParameter('bitbag_sylius_wishlist_plugin.parameters.anonymous_wishlist_expiration_period');
37+
38+
if (empty($expirationTime)) {
39+
$output->writeln('<error>`bitbag_sylius_wishlist_plugin.parameters.anonymous_wishlist_expiration_period` configuration key is not set, so no wishlists will be removed.</error>');
40+
41+
return 0;
42+
}
43+
44+
$output->writeln(sprintf(
45+
'Command will remove anonymous wishlists that have been idle for <info>%s</info>.',
46+
(string) $expirationTime,
47+
));
48+
49+
/** @var AnonymousWishlistsRemoverInterface $anonymousWishlistsRemover */
50+
$anonymousWishlistsRemover = $this->getContainer()->get('bitbag_sylius_wishlist_plugin.services.anonymous_wishlists_remover');
51+
$anonymousWishlistsRemover->remove();
52+
53+
return 0;
54+
}
55+
}

src/DependencyInjection/BitBagSyliusWishlistExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function load(array $config, ContainerBuilder $container): void
2929
$this->registerResources('bitbag_sylius_wishlist_plugin', 'doctrine/orm', $config['resources'], $container);
3030
$loader->load('services.yml');
3131
$container->setParameter('bitbag_sylius_wishlist_plugin.parameters.wishlist_cookie_token', $config['wishlist_cookie_token']);
32+
$container->setParameter('bitbag_sylius_wishlist_plugin.parameters.anonymous_wishlist_expiration_period', $config['anonymous_wishlist_expiration_period']);
3233
$container->setParameter('bitbag_sylius_wishlist_plugin.parameters.allowed_mime_types', $config['allowed_mime_types']);
3334
}
3435

src/DependencyInjection/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ public function getConfigTreeBuilder(): TreeBuilder
4545
})
4646
->end()
4747
->end()
48+
->scalarNode('anonymous_wishlist_expiration_period')
49+
->defaultValue('30 days')
50+
->cannotBeEmpty()
51+
->validate()
52+
->always(function ($value) {
53+
if (!is_string($value)) {
54+
throw new InvalidConfigurationException('anonymous_wishlist_expiration_period must be string');
55+
}
56+
57+
return $value;
58+
})
59+
->end()
60+
->end()
4861
->arrayNode('allowed_mime_types')
4962
->defaultValue([
5063
'text/csv',
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on [email protected].
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusWishlistPlugin\Remover;
13+
14+
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
15+
use Doctrine\Persistence\ObjectManager;
16+
17+
final class AnonymousWishlistsRemover implements AnonymousWishlistsRemoverInterface
18+
{
19+
public function __construct(
20+
private WishlistRepositoryInterface $wishlistRepository,
21+
private ObjectManager $wishlistManager,
22+
private ?string $expirationPeriod,
23+
) {
24+
}
25+
26+
public function remove(): void
27+
{
28+
$this->wishlistRepository->deleteAllAnonymousUntil(
29+
new \DateTime('-' . $this->expirationPeriod),
30+
);
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on [email protected].
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusWishlistPlugin\Remover;
13+
14+
interface AnonymousWishlistsRemoverInterface
15+
{
16+
public function remove(): void;
17+
}

src/Repository/WishlistRepository.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,16 @@ public function findOneByShopUserAndName(ShopUserInterface $shopUser, string $na
145145
->getOneOrNullResult()
146146
;
147147
}
148+
149+
public function deleteAllAnonymousUntil(\DateTime $until): int
150+
{
151+
return $this->createQueryBuilder('o')
152+
->delete(WishlistInterface::class, 'o')
153+
->where('o.shopUser IS NULL')
154+
->andWhere('o.updatedAt < :until')
155+
->setParameter('until', $until)
156+
->getQuery()
157+
->execute()
158+
;
159+
}
148160
}

src/Repository/WishlistRepositoryInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public function findAllByAnonymousAndChannel(?string $token, ChannelInterface $c
4040
public function findOneByTokenAndName(string $token, string $name): ?WishlistInterface;
4141

4242
public function findOneByShopUserAndName(ShopUserInterface $shopUser, string $name): ?WishlistInterface;
43+
44+
/**
45+
* @return int Number of deleted wishlists.
46+
*/
47+
public function deleteAllAnonymousUntil(\DateTime $until): int;
4348
}

src/Resources/config/services.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ imports:
22
- { resource: "services/**/*.yml" }
33

44
services:
5+
bitbag_sylius_wishlist_plugin.console.remove_anonymous_wishlists_command:
6+
class: BitBag\SyliusWishlistPlugin\Console\RemoveAnonymousWishlistsCommand
7+
tags:
8+
- { name: console.command }
9+
510
bitbag_sylius_wishlist_plugin.controller.action.base_wishlist_products_action:
611
abstract: true
712
class: BitBag\SyliusWishlistPlugin\Controller\Action\BaseWishlistProductsAction
@@ -348,6 +353,14 @@ services:
348353
- '@request_stack'
349354
- "@translator"
350355

356+
bitbag_sylius_wishlist_plugin.services.anonymous_wishlists_remover:
357+
public: true
358+
class: BitBag\SyliusWishlistPlugin\Remover\AnonymousWishlistsRemover
359+
arguments:
360+
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
361+
- "@bitbag_sylius_wishlist_plugin.manager.wishlist"
362+
- "%bitbag_sylius_wishlist_plugin.parameters.anonymous_wishlist_expiration_period%"
363+
351364
bitbag_sylius_wishlist_plugin.checker.wishlist_name_checker:
352365
class: BitBag\SyliusWishlistPlugin\Checker\WishlistNameChecker
353366

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Sylius\Component\Locale\Model\Locale:
2+
locale:
3+
createdAt: '<dateTimeBetween("-200 days", "now")>'
4+
code: 'en_US'
5+
Sylius\Component\Currency\Model\Currency:
6+
dollar:
7+
code: 'USD'
8+
Sylius\Component\Core\Model\Channel:
9+
channel_us:
10+
code: 'US'
11+
name: 'name'
12+
defaultLocale: '@locale'
13+
locales: [ '@locale' ]
14+
taxCalculationStrategy: 'order_items_based'
15+
baseCurrency: '@dollar'
16+
enabled: true
17+
Sylius\Component\Core\Model\Customer:
18+
customer_oliver:
19+
firstName: 'John'
20+
lastName: 'Nowak'
21+
22+
emailCanonical: '[email protected]'
23+
Sylius\Component\Core\Model\ShopUser:
24+
user_oliver:
25+
plainPassword: '123password'
26+
roles: [ 'ROLE_USER' ]
27+
enabled: 'true'
28+
customer: '@customer_oliver'
29+
username: '[email protected]'
30+
usernameCanonical: '[email protected]'
31+
BitBag\SyliusWishlistPlugin\Entity\Wishlist:
32+
wishlist_one:
33+
name: 'Wishlist One'
34+
channel: '@channel_us'
35+
token: 'token'
36+
updatedAt: '<date_create_from_format("Y-m-d H:i:s", "2023-01-01 00:00:00")>'
37+
wishlist_two:
38+
name: 'Wishlist Two'
39+
channel: '@channel_us'
40+
token: 'token'
41+
updatedAt: '<date_create_from_format("Y-m-d H:i:s", "2024-01-02 00:00:00")>'
42+
olivier_wishlist:
43+
name: 'Olivier Wishlist'
44+
shopUser: '@user_oliver'
45+
channel: '@channel_us'
46+
updatedAt: '<date_create_from_format("Y-m-d H:i:s", "2023-01-01 00:00:00")>'

0 commit comments

Comments
 (0)