<?php
namespace App\Controller;
use App\Entity\Security\Users;
use App\Services\MailService;
use App\Services\SerializationService;
use App\Services\TestService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Security;
class DefaultController extends AbstractController
{
private $em;
private $serialization;
private $passwordHasher;
private $tokenStorage;
private $testService;
private $serializationService;
private $mailService;
public function __construct(
EntityManagerInterface $entityManager,
UserPasswordHasherInterface $passwordHasher,
Security $security,
TestService $testService,
SerializationService $serializationService,
MailService $mailService,
TokenStorageInterface $tokenStorage)
{
$this->em = $entityManager;
$this->passwordHasher = $passwordHasher;
$this->tokenStorage = $tokenStorage;
$this->testService = $testService;
$this->serializationService = $serializationService;
$this->mailService = $mailService;
}
/**
* @Route("/", name="index")
*/
public function index()
{
return $this->render("index.html.twig");
}
/**
* @Route("/auth_test", name="auth_test")
*/
public function auth_test()
{
$em = $this->em;
$User = $em->getRepository(Users::class)->findOneBy(array('username' => 'admin'));
if(is_object($User)){
$token = new UsernamePasswordToken($User, 'main',$User->getRoles());
$this->tokenStorage->setToken($token);
}
return $this->render("index.html.twig");
}
/**
* @Route("/auth_create", name="auth_create")
*/
public function auth_create()
{
$em = $this->em;
$User = new Users();
$User->setFullName("admin");
$User->setUsername("admin");
$User->setEmail("admin@example.com");
$User->setRoles(array("ROLE_ADMIN"));
$plaintextPassword = "123123";
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $this->passwordHasher->hashPassword($User, $plaintextPassword);
$User->setPassword($hashedPassword);
$em->persist($User);
$em->flush();
return $this->render("index.html.twig");
}
/**
* @Route("/test_service", name="test_service")
*/
public function test_service()
{
$em = $this->em;
$res = $this->testService->hello();
echo $res;
die();
return $this->render("index.html.twig");
}
/**
* @Route("/test_serialization", name="test_serialization")
*/
public function test_serialization()
{
$em = $this->em;
$users = $em->getRepository(Users::class)->findAll();
$dataUsers = $this->serializationService->serialize($users);
echo "<pre>";
var_export($dataUsers);
die();
}
/**
* @Route("/test_mail", name="test_serialization")
*/
public function test_mail()
{
$arrayParamMail = array(
'full_name' => 'Tarik SERRAKH',
'code' => '123123'
);
$body = $this->renderView('mailing/demos/mail_demo.html.twig', $arrayParamMail);
$this->mailService->setSubject('Test TRK Objet');
//$this->mailService->setText('Hello TRK');
$this->mailService->setBody($body);
$this->mailService->setFrom('noreplay@e-ambition.com', 'E-AMBITION');
$this->mailService->addAddress('tarik.serrakh@gmail.com', 'Tarik SERRAKH');
$this->mailService->addAddress('eambition.sarl@gmail.com', 'EA');
//$this->mailService->addAttachment('https://cdn.tictacsante.com/assets/images/doc/covers/cover-02DSQ454D5S4DD5Q4.jpg');
$this->mailService->Dispatcher();
return $this->render("index.html.twig");
}
}