PHP code example of tourze / user-agreement-bundle

1. Go to this page and download the library: Download tourze/user-agreement-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / user-agreement-bundle example snippets


return [
    // ...
    UserAgreementBundle\UserAgreementBundle::class => ['all' => true],
];

use UserAgreementBundle\Entity\ProtocolEntity;
use UserAgreementBundle\Enum\ProtocolType;

$protocol = new ProtocolEntity();
$protocol->setType(ProtocolType::PRIVACY);
$protocol->setTitle('Privacy Policy');
$protocol->setVersion('1.0');
$protocol->setContent('Your privacy policy content...');
$protocol->setRequired(true);
$protocol->setEffectiveTime(new \DateTimeImmutable());

$entityManager->persist($protocol);
$entityManager->flush();

use UserAgreementBundle\Entity\AgreeLog;

$agreeLog = new AgreeLog();
$agreeLog->setMemberId($user->getId());
$agreeLog->setProtocolId($protocol->getId());
$agreeLog->setValid(true);

$entityManager->persist($agreeLog);
$entityManager->flush();

use UserAgreementBundle\Entity\RevokeRequest;
use UserAgreementBundle\Enum\RevokeType;

$revokeRequest = new RevokeRequest();
$revokeRequest->setUser($user);
$revokeRequest->setType(RevokeType::All);
$revokeRequest->setRemark('User requested account deletion');

$entityManager->persist($revokeRequest);
$entityManager->flush();

use Symfony\Component\Validator\Constraints as Assert;

#[Assert\Callback]
public function validateProtocol(ExecutionContextInterface $context): void
{
    if ($this->type === ProtocolType::PRIVACY && empty($this->content)) {
        $context->buildViolation('Privacy policy content is 

use UserAgreementBundle\Event\AgreeProtocolEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ProtocolSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            AgreeProtocolEvent::class => 'onProtocolAgreed',
        ];
    }

    public function onProtocolAgreed(AgreeProtocolEvent $event): void
    {
        // Custom logic when user agrees to protocol
        $protocol = $event->getProtocol();
        $user = $event->getUser();
        
        // Send confirmation email, log activity, etc.
    }
}

use UserAgreementBundle\Service\MemberService;

class MyService
{
    public function __construct(
        private MemberService $memberService,
    ) {}
    
    public function processUser(UserInterface $user): void
    {
        $memberId = $this->memberService->extractMemberId($user);
        // Process user agreement logic
    }
}
bash
php bin/console doctrine:migrations:migrate