vendor/avenue/avenue-admin/src/Controller/BaseController.php line 89

Open in your IDE?
  1. <?php
  2. namespace AvenueAdminBundle\Controller;
  3. use AvenueAdminBundle\Document\Config;
  4. use AvenueAdminBundle\Mail\Mail;
  5. use Psr\Log\LoggerInterface;
  6. use Doctrine\ODM\MongoDB\DocumentManager;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher;
  9. use Symfony\Component\Security\Core\Security;
  10. use Doctrine\ODM\MongoDB\Query\Builder;
  11. use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. abstract class BaseController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
  15. {
  16.     /**
  17.      * @var DocumentManager
  18.      */
  19.     private $dm;
  20.     private $logger;
  21.     /**
  22.      * @return LoggerInterface
  23.      */
  24.     public function getLogger()
  25.     {
  26.         return $this->logger;
  27.     }
  28.     /**
  29.      * @param LoggerInterface $logger
  30.      * @required
  31.      */
  32.     public function setLogger(LoggerInterface $logger): void
  33.     {
  34.         $this->logger $logger;
  35.     }
  36.     /**
  37.      * @return DocumentManager
  38.      */
  39.     public function getDocumentManager() {
  40.         return $this->dm;
  41.     }
  42.     /**
  43.      * @param DocumentManager $dm
  44.      * @required
  45.      */
  46.     public function setDocumentManager(DocumentManager $dm): void
  47.     {
  48.         $this->dm $dm;
  49.     }
  50.     /**
  51.      * @param $className
  52.      * @return \Doctrine\Common\Persistence\ObjectRepository|DocumentRepository
  53.      */
  54.     protected function getDocumentRepository($className)
  55.     {
  56.         return $this->dm->getRepository($className);
  57.     }
  58.     /**
  59.      * @param $className
  60.      * @return Builder
  61.      */
  62.     protected function getQueryBuilder($className)
  63.     {
  64.         return $this->getDocumentRepository($className)->createQueryBuilder();
  65.     }
  66.     /**
  67.      * @return \Twig\Environment
  68.      */
  69.     protected function getTwig()
  70.     {
  71.         return $this->container->get('twig');
  72.     }
  73.     /**
  74.      * @param string $seoTitle
  75.      * @param string $seoDescription
  76.      * @param string $seoKeywords
  77.      */
  78.     protected function setSeoVariables($seoTitle$seoDescription ''$seoKeywords '')
  79.     {
  80.         $twig $this->getTwig();
  81.         $twig->addGlobal('seoTitle'$seoTitle);
  82.         $twig->addGlobal('seoDescription'$seoDescription);
  83.         $twig->addGlobal('seoKeywords'$seoKeywords);
  84.     }
  85.     private $mail;
  86.     /**
  87.      * @required
  88.      * @param Mail $mail
  89.      */
  90.     public function setMail(Mail $mail) {
  91.         $this->mail $mail;
  92.     }
  93.     public function getMail(): Mail {
  94.         return $this->mail;
  95.     }
  96.     /**
  97.      * @return string
  98.      */
  99.     protected function generatePassword()
  100.     {
  101.         $chars "qazxswedcvfrtgbnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP";
  102.         $passwordSize $this->getDefaultPasswordSize();
  103.         $size strlen($chars) - 1;
  104.         $password null;
  105.         while ($passwordSize--) {
  106.             $password .= $chars[rand(0$size)];
  107.         }
  108.         return $password;
  109.     }
  110.     protected function getPasswordHash($password)
  111.     {
  112.         $nativePasswordEncoder = new NativePasswordHasher();
  113.         $passwordAsHash $nativePasswordEncoder->hash($password);
  114.         if ($passwordAsHash === false) {
  115.             throw new \Exception('Error occurred while encoding password');
  116.         }
  117.         return $passwordAsHash;
  118.     }
  119.     /**
  120.      * @return int
  121.      */
  122.     protected function getDefaultPasswordSize()
  123.     {
  124.         return 10;
  125.     }
  126.     protected function createBadRequestResponse(string $message, array $data = [])
  127.     {
  128.         return new JsonResponse(array_merge($data, ['detail' => $message]), 400);
  129.     }
  130. }