PHP code example of tourze / user-level-bundle

1. Go to this page and download the library: Download tourze/user-level-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-level-bundle example snippets


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

use UserLevelBundle\Entity\Level;

$level = new Level();
$level->setTitle('Bronze');
$level->setLevel(1);
$level->setValid(true);
$entityManager->persist($level);
$entityManager->flush();

use UserLevelBundle\Entity\UpgradeRule;

$rule = new UpgradeRule();
$rule->setTitle('Points to Bronze');
$rule->setValue(100);
$rule->setLevel($level);
$rule->setValid(true);
$entityManager->persist($rule);
$entityManager->flush();

use UserLevelBundle\Service\UserLevelUpgradeService;

class UserController
{
    public function __construct(
        private readonly UserLevelUpgradeService $userLevelUpgradeService
    ) {
    }

    public function upgradeUser(UserInterface $user): void
    {
        $this->userLevelUpgradeService->upgrade($user);
    }
}

use UserLevelBundle\Repository\LevelRepository;
use UserLevelBundle\Repository\UserLevelRelationRepository;

class UserLevelController
{
    public function __construct(
        private readonly LevelRepository $levelRepository,
        private readonly UserLevelRelationRepository $relationRepository
    ) {
    }

    public function getUserLevel(UserInterface $user): ?Level
    {
        $relation = $this->relationRepository->findOneBy(['user' => $user, 'valid' => true]);
        return $relation?->getLevel();
    }
}

use UserLevelBundle\Service\UserLevelUpgradeService;

class CustomUpgradeService extends UserLevelUpgradeService
{
    public function checkCustomUpgradeConditions(UserInterface $user): bool
    {
        // Implement your custom logic here
        return true;
    }
}
bash
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
bash
php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/user-level-bundle