Skip to content

Uniformisation de l'instanciation du service Cotisations #1863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,6 @@ services:
$algoliaAppId: '%algolia_app_id%'
$algoliaFrontendApikey: '%algolia_frontend_api_key%'

AppBundle\Controller\Website\MemberShipController:
autowire: true
autoconfigure: true
public: false
arguments:
$storageDir: '%app.general_meetings_dir%'
$formLoginAuthenticator: '@security.authenticator.form_login.legacy_secured_area'

AppBundle\Controller\Website\NewsletterController:
autowire: true
autoconfigure: true
Expand Down Expand Up @@ -311,12 +303,11 @@ services:
arguments:
- 'Afup\Site\Forum\Inscriptions'

Afup\Site\Association\CotisationsFactory:
autowire: true

Afup\Site\Association\Cotisations:
factory: ['@AppBundle\LegacyModelFactory', 'createObject']
arguments:
- 'Afup\Site\Association\Cotisations'
calls:
- [setCompanyMemberRepository, ['@AppBundle\Association\Model\Repository\CompanyMemberRepository']]
factory: ['@Afup\Site\Association\CotisationsFactory', 'create']

Parsedown:
class: Parsedown
Expand Down
31 changes: 31 additions & 0 deletions sources/Afup/Association/CotisationsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Afup\Site\Association;

use Afup\Site\Utils\Utils;
use AppBundle\Association\Model\Repository\CompanyMemberRepository;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

final readonly class CotisationsFactory
{
public function __construct(
private TokenStorageInterface $tokenStorage,
private CompanyMemberRepository $companyMemberRepository,
private AuthorizationCheckerInterface $authorizationChecker,
) {}

public function create(): Cotisations
{
$cotisations = new Cotisations(
$GLOBALS['AFUP_DB'],
Utils::fabriqueDroits($this->tokenStorage, $this->authorizationChecker),
);

$cotisations->setCompanyMemberRepository($this->companyMemberRepository);

return $cotisations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@
use Afup\Site\Association\Cotisations;
use Afup\Site\Utils\Utils;
use AppBundle\Association\Model\CompanyMember;
use AppBundle\LegacyModelFactory;

class SubscriptionManagement
final readonly class SubscriptionManagement
{
public function __construct(private readonly LegacyModelFactory $legacyModelFactory) {}
public function __construct(private Cotisations $cotisations) {}

public function createInvoiceForInscription(CompanyMember $company, $numberOfMembers): array
{
$subscription = $this->legacyModelFactory->createObject(Cotisations::class);

$endSubscription = $subscription->finProchaineCotisation(false);
$endSubscription = $this->cotisations->finProchaineCotisation(false);

// Create the invoice
$subscription->ajouter(
$this->cotisations->ajouter(
AFUP_PERSONNES_MORALES,
$company->getId(),
ceil($numberOfMembers / AFUP_PERSONNE_MORALE_SEUIL) * AFUP_COTISATION_PERSONNE_MORALE * (1 + Utils::MEMBERSHIP_FEE_VAT_RATE),
Expand All @@ -30,7 +27,7 @@ public function createInvoiceForInscription(CompanyMember $company, $numberOfMem
$endSubscription->format('U'),
'',
);
$subscriptionArray = $subscription->obtenirDerniere(AFUP_PERSONNES_MORALES, $company->getId());
$subscriptionArray = $this->cotisations->obtenirDerniere(AFUP_PERSONNES_MORALES, $company->getId());

if ($subscriptionArray === false) {
throw new \RuntimeException('An error occured');
Expand Down
53 changes: 21 additions & 32 deletions sources/AppBundle/Controller/Website/MemberShipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use AppBundle\Twig\ViewRenderer;
use Assert\Assertion;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand Down Expand Up @@ -78,6 +79,7 @@ public function __construct(
private CompanyMemberRepository $companyMemberRepository,
private UserService $userService,
private UserAuthenticatorInterface $userAuthenticator,
#[Autowire('@security.authenticator.form_login.legacy_secured_area')]
private FormLoginAuthenticator $formLoginAuthenticator,
private InvitationMail $invitationMail,
private SubscriptionManagement $subscriptionManagement,
Expand All @@ -88,12 +90,14 @@ public function __construct(
private GeneralMeetingRepository $generalMeetingRepository,
private GeneralMeetingQuestionRepository $generalMeetingQuestionRepository,
private GeneralMeetingVoteRepository $generalMeetingVoteRepository,
#[Autowire('%app.general_meetings_dir%')]
private string $storageDir,
private UserPasswordHasherInterface $passwordHasher,
private CompanyMemberInvitationRepository $companyMemberInvitationRepository,
private TechletterSubscriptionsRepository $techletterSubscriptionsRepository,
private TechletterUnsubscriptionsRepository $techletterUnsubscriptionsRepository,
private SendingRepository $sendingRepository,
private Cotisations $cotisations,
) {}

public function becomeMember(): Response
Expand Down Expand Up @@ -183,11 +187,7 @@ public function company(Request $request)

public function payment(string $invoiceNumber, ?string $token): Response
{
/**
* @var Cotisations $subscription
*/
$subscription = $this->legacyModelFactory->createObject(Cotisations::class);
$invoice = $subscription->getByInvoice($invoiceNumber, $token);
$invoice = $this->cotisations->getByInvoice($invoiceNumber, $token);
$company = $this->companyMemberRepository->get($invoice['id_personne']);

if (!$invoice || $company === null) {
Expand Down Expand Up @@ -220,18 +220,14 @@ public function payment(string $invoiceNumber, ?string $token): Response

public function invoice(string $invoiceNumber, ?string $token): Response
{
/**
* @var Cotisations $subscription
*/
$subscription = $this->legacyModelFactory->createObject(Cotisations::class);
$invoice = $subscription->getByInvoice($invoiceNumber, $token);
$invoice = $this->cotisations->getByInvoice($invoiceNumber, $token);

if (!$invoice) {
throw $this->createNotFoundException(sprintf('Could not find the invoice "%s" with token "%s"', $invoiceNumber, $token));
}

ob_start();
$subscription->genererFacture($invoice['id']);
$this->cotisations->genererFacture($invoice['id']);
$pdf = ob_get_clean();

$response = new Response($pdf);
Expand Down Expand Up @@ -309,11 +305,7 @@ public function slackInviteRequest(): RedirectResponse
public function payboxCallback(Request $request)
{
$payboxResponse = PayboxResponseFactory::createFromRequest($request);
/**
* @var Cotisations $cotisations
*/
$cotisations = $this->legacyModelFactory->createObject(Cotisations::class);
$cotisations->setCompanyMemberRepository($this->companyMemberRepository);
$this->cotisations->setCompanyMemberRepository($this->companyMemberRepository);
$logs = $this->legacyModelFactory->createObject(Logs::class);

$status = $payboxResponse->getStatus();
Expand All @@ -331,16 +323,16 @@ public function payboxCallback(Request $request)
}

if ($etat == AFUP_COTISATIONS_PAIEMENT_REGLE) {
$account = $cotisations->getAccountFromCmd($payboxResponse->getCmd());
$lastCotisation = $cotisations->obtenirDerniere($account['type'], $account['id']);
$account = $this->cotisations->getAccountFromCmd($payboxResponse->getCmd());
$lastCotisation = $this->cotisations->obtenirDerniere($account['type'], $account['id']);

if ($lastCotisation === false && $account['type'] == UserRepository::USER_TYPE_PHYSICAL) {
$user = $this->userRepository->get($account['id']);
$this->eventDispatcher->dispatch(new NewMemberEvent($user));
}

$cotisations->validerReglementEnLigne($payboxResponse->getCmd(), round($payboxResponse->getTotal() / 100, 2), $payboxResponse->getAuthorizationId(), $payboxResponse->getTransactionId());
$cotisations->notifierReglementEnLigneAuTresorier($payboxResponse->getCmd(), round($payboxResponse->getTotal() / 100, 2), $payboxResponse->getAuthorizationId(), $payboxResponse->getTransactionId(), $this->userRepository);
$this->cotisations->validerReglementEnLigne($payboxResponse->getCmd(), round($payboxResponse->getTotal() / 100, 2), $payboxResponse->getAuthorizationId(), $payboxResponse->getTransactionId());
$this->cotisations->notifierReglementEnLigneAuTresorier($payboxResponse->getCmd(), round($payboxResponse->getTotal() / 100, 2), $payboxResponse->getAuthorizationId(), $payboxResponse->getTransactionId(), $this->userRepository);
$logs::log("Ajout de la cotisation " . $payboxResponse->getCmd() . " via Paybox.");
}
return new Response();
Expand Down Expand Up @@ -389,7 +381,6 @@ private function getDroits(): Droits
public function membershipFee(): Response
{
$userService = $this->userService;
$cotisations = $this->getCotisations();

$identifiant = $this->getDroits()->obtenirIdentifiant();
$user = $this->userRepository->get($identifiant);
Expand All @@ -401,7 +392,7 @@ public function membershipFee(): Response
if (!$cotisation) {
$message = '';
} else {
$endSubscription = $cotisations->finProchaineCotisation($cotisation);
$endSubscription = $this->cotisations->finProchaineCotisation($cotisation);
$message = sprintf(
'Votre dernière cotisation -- %s %s -- est valable jusqu\'au %s. <br />
Si vous renouvelez votre cotisation maintenant, celle-ci sera valable jusqu\'au %s.',
Expand All @@ -412,8 +403,8 @@ public function membershipFee(): Response
);
}

$cotisations_physique = $cotisations->obtenirListe(0, $user->getId());
$cotisations_morale = $cotisations->obtenirListe(1, $user->getCompanyId());
$cotisations_physique = $this->cotisations->obtenirListe(0, $user->getId());
$cotisations_morale = $this->cotisations->obtenirListe(1, $user->getCompanyId());

if (is_array($cotisations_morale) && is_array($cotisations_physique)) {
$liste_cotisations = array_merge($cotisations_physique, $cotisations_morale);
Expand All @@ -426,7 +417,7 @@ public function membershipFee(): Response
}

foreach ($liste_cotisations as $k => $cotisation) {
$liste_cotisations[$k]['telecharger_facture'] = $cotisations->isCurrentUserAllowedToReadInvoice($cotisation['id']);
$liste_cotisations[$k]['telecharger_facture'] = $this->cotisations->isCurrentUserAllowedToReadInvoice($cotisation['id']);
}

if ($user->getCompanyId() > 0) {
Expand Down Expand Up @@ -485,20 +476,19 @@ private function getCotisations(): Cotisations

public function membershipFeeDownload(Request $request): BinaryFileResponse
{
$cotisations = $this->getCotisations();
$identifiant = $this->getDroits()->obtenirIdentifiant();
$id = $request->get('id');

$logs = $this->legacyModelFactory->createObject(Logs::class);

if (false === $cotisations->isCurrentUserAllowedToReadInvoice($id)) {
if (false === $this->cotisations->isCurrentUserAllowedToReadInvoice($id)) {
$logs::log("L'utilisateur id: " . $identifiant . ' a tenté de voir la facture id:' . $id);
throw $this->createAccessDeniedException('Cette facture ne vous appartient pas, vous ne pouvez la visualiser.');
}

$tempfile = tempnam(sys_get_temp_dir(), 'membership_fee_download');
$numeroFacture = $cotisations->genererFacture($id, $tempfile);
$cotisation = $cotisations->obtenir($id);
$numeroFacture = $this->cotisations->genererFacture($id, $tempfile);
$cotisation = $this->cotisations->obtenir($id);

if ($cotisation['type_personne'] == AFUP_PERSONNES_MORALES) {
$company = $this->companyMemberRepository->get($cotisation['id_personne']);
Expand All @@ -521,19 +511,18 @@ public function membershipFeeDownload(Request $request): BinaryFileResponse

public function membershipFeeSendMail(Request $request): RedirectResponse
{
$cotisations = $this->getCotisations();
$identifiant = $this->getDroits()->obtenirIdentifiant();
$id = $request->get('id');

$logs = $this->legacyModelFactory->createObject(Logs::class);
$userRepository = $this->userRepository;

if (false === $cotisations->isCurrentUserAllowedToReadInvoice($id)) {
if (false === $this->cotisations->isCurrentUserAllowedToReadInvoice($id)) {
$logs::log("L'utilisateur id: " . $identifiant . ' a tenté de voir la facture id:' . $id);
throw $this->createAccessDeniedException('Cette facture ne vous appartient pas, vous ne pouvez la visualiser.');
}

if ($cotisations->envoyerFacture($id, $this->mailer, $userRepository)) {
if ($this->cotisations->envoyerFacture($id, $this->mailer, $userRepository)) {
$logs::log('Envoi par email de la facture pour la cotisation n°' . $id);
$this->addFlash('success', 'La facture a été envoyée par mail');
} else {
Expand Down