Skip to content

Commit

Permalink
Add command to get voucher count for a user
Browse files Browse the repository at this point in the history
  • Loading branch information
y3n4 committed Feb 6, 2025
1 parent f958fa8 commit 60aaa7d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 31 deletions.
64 changes: 33 additions & 31 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,51 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
locale: 'en'
locales: 'en|de|es|fr|it|pt|nb|gsw'
supported_locales: ['en', 'de', 'es', 'fr', 'it', 'pt', 'nb', 'gsw']
locale: "en"
locales: "en|de|es|fr|it|pt|nb|gsw"
supported_locales: ["en", "de", "es", "fr", "it", "pt", "nb", "gsw"]
navbar_left:
-
name: 'navbar_left.about-us.text'
url: 'navbar_left.about-us.link'

- name: "navbar_left.about-us.text"
url: "navbar_left.about-us.link"

services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
resource: "../src/"
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- "../src/DependencyInjection/"
- "../src/Entity/"
- "../src/Kernel.php"

# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
resource: "../src/Controller/"
tags: ["controller.service_arguments"]

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

App\Builder\AliasCreatedMessageBuilder:
arguments:
$appUrl: "%env(APP_URL)%"
$projectName: "%env(PROJECT_NAME)%"
$appUrl: "%env(APP_URL)%"
$projectName: "%env(PROJECT_NAME)%"

App\Builder\RecoveryProcessMessageBuilder:
arguments:
$appUrl: "%env(APP_URL)%"
$projectName: "%env(PROJECT_NAME)%"
$appUrl: "%env(APP_URL)%"
$projectName: "%env(PROJECT_NAME)%"

App\Builder\WelcomeMessageBuilder:
arguments:
$appUrl: "%env(APP_URL)%"
$projectName: "%env(PROJECT_NAME)%"
$appUrl: "%env(APP_URL)%"
$projectName: "%env(PROJECT_NAME)%"

App\Command\UsersCheckPasswordCommand:
arguments:
Expand All @@ -75,6 +73,10 @@ services:
arguments:
$mailLocation: "%env(DOVECOT_MAIL_LOCATION)%"

App\Command\VoucherCountCommand:
arguments:
$appUrl: "%env(APP_URL)%"

App\Command\VoucherCreateCommand:
arguments:
$appUrl: "%env(APP_URL)%"
Expand All @@ -87,7 +89,7 @@ services:
$mailGid: "%env(DOVECOT_MAIL_GID)%"

App\EventListener\:
resource: '../src/EventListener/*'
resource: "../src/EventListener/*"
tags:
- { name: kernel.event_subscriber }

Expand All @@ -97,7 +99,7 @@ services:
$supportedLocales: "%supported_locales%"

App\Handler\:
resource: '../src/Handler/*'
resource: "../src/Handler/*"
public: true

App\Form\PasswordChangeType:
Expand Down Expand Up @@ -129,15 +131,15 @@ services:
public: true
arguments:
$registrationOpen: "%env(REGISTRATION_OPEN)%"
$mailCrypt: '%env(MAIL_CRYPT)%'
$mailCrypt: "%env(MAIL_CRYPT)%"

App\Handler\SuspiciousChildrenHandler:
arguments:
$to: "%env(NOTIFICATION_ADDRESS)%"

App\Handler\UserAuthenticationHandler:
arguments:
$passwordHasherFactory: '@security.password_hasher_factory'
$passwordHasherFactory: "@security.password_hasher_factory"

App\Handler\WkdHandler:
arguments:
Expand All @@ -164,7 +166,7 @@ services:

App\Builder\MenuBuilder:
arguments:
$navbarLeft: '%navbar_left%'
$navbarLeft: "%navbar_left%"

App\Remover\VoucherRemover:
arguments:
Expand All @@ -173,10 +175,10 @@ services:

App\Security\ApiAccessTokenHandler:
arguments:
$accessTokenDovecot: '%env(DOVECOT_API_ACCESS_TOKEN)%'
$accessTokenKeycloak: '%env(KEYCLOAK_API_ACCESS_TOKEN)%'
$accessTokenRetention: '%env(RETENTION_API_ACCESS_TOKEN)%'
$accessTokenPostfix: '%env(POSTFIX_API_ACCESS_TOKEN)%'
$accessTokenDovecot: "%env(DOVECOT_API_ACCESS_TOKEN)%"
$accessTokenKeycloak: "%env(KEYCLOAK_API_ACCESS_TOKEN)%"
$accessTokenRetention: "%env(RETENTION_API_ACCESS_TOKEN)%"
$accessTokenPostfix: "%env(POSTFIX_API_ACCESS_TOKEN)%"

App\Sender\WelcomeMessageSender:
public: true
Expand Down Expand Up @@ -208,7 +210,7 @@ services:
class: App\Admin\DomainAdmin
public: true
tags:
- { name: sonata.admin, manager_type: orm, group: "Mail", label: "Domain" }
- { name: sonata.admin, manager_type: orm, group: "Mail", label: "Domain" }
arguments:
- ~
- App\Entity\Domain
Expand All @@ -221,7 +223,7 @@ services:
class: App\Admin\AliasAdmin
public: true
tags:
- { name: sonata.admin, manager_type: orm, group: "Mail", label: "Alias" }
- { name: sonata.admin, manager_type: orm, group: "Mail", label: "Alias" }
arguments:
- ~
- App\Entity\Alias
Expand Down
55 changes: 55 additions & 0 deletions src/Command/VoucherCountCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Command;

use App\Entity\User;
use App\Entity\Voucher;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;

#[AsCommand(name: 'app:voucher:count')]
class VoucherCountCommand extends Command
{
public function __construct(
private readonly EntityManagerInterface $manager,
private readonly RouterInterface $router,
private readonly string $appUrl
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Get count of unredeemed vouchers for a specific user')
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'User whose vouchers are counted')
->addOption('redeemed', 'p', InputOption::VALUE_NONE, 'Show count of redeemed vouchers');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$email = $input->getOption('user');

$context = $this->router->getContext();
$context->setBaseUrl($this->appUrl);

if (empty($email) || null === $user = $this->manager->getRepository(User::class)->findByEmail($email)) {
throw new UserNotFoundException(sprintf('User with email %s not found!', $email));
}

if (false === $input->getOption('redeemed')) {
$count = $this->manager->getRepository(Voucher::class)->countUnredeemedVouchersByUser($user);
$output->write(sprintf($count));
} else {
$count = $this->manager->getRepository(Voucher::class)->countRedeemedVouchersByUser($user);
$output->write(sprintf($count));
}
return 0;
}
}
23 changes: 23 additions & 0 deletions src/Repository/VoucherRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ public function countUnredeemedVouchers(): int
return $this->matching(Criteria::create()->where(Criteria::expr()->eq('redeemedTime', null)))->count();
}


/**
* Returns the number of redeemed vouchers per user.
*/
public function countRedeemedVouchersByUser(User $user): int
{
return $this->matching(Criteria::create()
->where(Criteria::expr()->eq('user', $user))
->andWhere(Criteria::expr()->neq('redeemedTime', null)))
->count();
}

/**
* Returns the number of unredeemed vouchers per user.
*/
public function countUnredeemedVouchersByUser(User $user): int
{
return $this->matching(Criteria::create()
->where(Criteria::expr()->eq('user', $user))
->andWhere(Criteria::expr()->eq('redeemedTime', null)))
->count();
}

/**
* Finds all vouchers for a given user.
*
Expand Down

0 comments on commit 60aaa7d

Please sign in to comment.