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 4c7293f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
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 4c7293f

Please sign in to comment.