src/EventSubscriber/LicenseValidationListener.php line 24

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Filesystem\Filesystem;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  8. class LicenseValidationListener implements EventSubscriberInterface
  9. {
  10.     private $hasher;
  11.     private $filesystem;
  12.     public function __construct(UserPasswordHasherInterface $hasher)
  13.     {
  14.         $this->hasher $hasher;
  15.         $this->filesystem = new Filesystem();
  16.     }
  17.     public function onKernelController(ControllerEvent $event)
  18.     {
  19.         $encryptedCode $this->getEncryptedCode();
  20.         if ($this->isValidLicenseCode($encryptedCode)) {
  21.             // Continue with normal execution
  22.         } else {
  23.             throw new \RuntimeException('Invalid license code.');
  24.         }
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             KernelEvents::CONTROLLER => 'onKernelController',
  30.         ];
  31.     }
  32.     private function isValidLicenseCode(string $encryptedCode): bool
  33.     {
  34.         // Your license code validation logic goes here
  35.         // Return true if the license code is valid, false otherwise
  36.         // For example:
  37.         return true;
  38.         return $encryptedCode === 'YOUR_LICENSE_CODE';
  39.     }
  40.     private function getEncryptedCode(): string
  41.     {
  42.         $rootDir $this->getProjectRootDir();
  43.         $encryptedFilePath $rootDir '/encrypted_license.txt';
  44.         if ($this->filesystem->exists($encryptedFilePath)) {
  45.             return trim(file_get_contents($encryptedFilePath));
  46.         }
  47.         throw new \RuntimeException('No encrypted license code found.');
  48.     }
  49.     private function getProjectRootDir(): string
  50.     {
  51.         return realpath(__DIR__ '/../..');
  52.     }
  53. }