<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
class WelcomeController extends AbstractController {
/**
* Zasób odczytuje stan konta w HRD i SMSAPI, zwraca stan JSONem, oraz modyfikuje zmienne środowiskowe.
* @Route ("/getBalance", name="get-balance", methods={"POST"})
* @isGranted ("ROLE_ADMIN")
* @param \App\Service\SMSApi $SMSAPI
* @param \App\Controller\Doctrine\ORM\EntityManagerInterface $EM
* @param \Symfony\Component\HttpKernel\KernelInterface $KERNEL
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function getBalance (\App\Service\SMSApi $SMSAPI, \Doctrine\ORM\EntityManagerInterface $EM): \Symfony\Component\HttpFoundation\JsonResponse {
$wynik = [
"HRD" => [
"AMOUNT" => "0,00 PLN",
"LOCK" => "0,00 PLN",
"AVAILABLE" => "0,00 PLN"
],
"SMSAPI" => 0
];
$HRD = \HRDBase\Api\HRDApi::getInstance([
"apiHash" => $EM->getRepository(\App\Entity\Conf::class)->peek("hash"),
"apiLogin" => $EM->getRepository(\App\Entity\Conf::class)->peek("login"),
"apiPass" => $EM->getRepository(\App\Entity\conf::class)->peek("pass")
]);
$HRDTOKEN = $HRD->getToken();
if ($HRDTOKEN) {
$balance = $HRD->partnerGetBalance();
$wynik ["HRD"]["AMOUNT"] = number_format (floatval ($balance ['balance']), 2, ",", " ") . " PLN";
$wynik ["HRD"]["LOCK"] = number_format (floatval ($balance ['restrictedBalance']), 2, ",", " ") . " PLN";
$wynik ["HRD"]["AVAILABLE"] = number_format (floatval ($balance ['balance']) + floatval ($balance ['restrictedBalance']), 2, ",", " ") . " PLN";
}
$wynik ['SMSAPI'] = $SMSAPI->getBalance($EM->getRepository(\App\Entity\Conf::class));
$envLines = file ("../.env");
foreach ($envLines as &$envLine) {
if (substr ($envLine, 0, 12) == "HRD_AMOUNT=\"") {
$envLine = "HRD_AMOUNT=\"" . $wynik ['HRD']['AMOUNT'] . "\"\n";
} elseif (substr ($envLine, 0, 10) == "HRD_LOCK=\"") {
$envLine = "HRD_LOCK=\"" . $wynik ['HRD']['LOCK'] . "\"\n";
} elseif (substr ($envLine, 0, 15) == "HRD_AVAILABLE=\"") {
$envLine = "HRD_AVAILABLE=\"" . $wynik ['HRD']['AVAILABLE'] . "\"\n";
} elseif (substr ($envLine, 0, 14) == "SMSAPI_POINTS=") {
$envLine = "SMSAPI_POINTS=\"" . $wynik ['SMSAPI'] . "\"\n";
}
}
file_put_contents ("../.env", implode ("", $envLines));
return new \Symfony\Component\HttpFoundation\JsonResponse($wynik);
}
/**
* @Route("/chuj")
* @param \Doctrine\ORM\EntityManagerInterface $manager
* @return Response
*/
function chuj (\Doctrine\ORM\EntityManagerInterface $manager): Response {
return $this->render (
"chuj.html.twig", [
"messages" => [
[
"body" => "Komunikat <b><a href='/'>1</a></b>",
],
[
"body" => "Komunikat <b>2</b>",
"type" => "success",
"icon" => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-power"><path d="M18.36 6.64a9 9 0 1 1-12.73 0"></path><line x1="12" y1="2" x2="12" y2="12"></line></svg>'
],
[
"body" => "Komunikat <b>3</b>",
"type" => "warning",
"icon" => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shield"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path></svg>'
],
[
"body" => "Komunikat <b>4</b>",
"type" => "danger"
]
]
]
);
}
/**
* @Route("/test")
* @return Response
*/
function testTemplate (): Response {
return $this->render("_test.html.twig");
}
/**
* @Route ("/cover");
* @return \App\Controller\response
*/
function testCover (): response {
return $this->render ('_cover.html.twig');
}
/**
* @Route("/", name="app_welcome")
*/
public function index(): Response
{
return $this->render('index.html.twig', [
'controller_name' => 'WelcomeController',
]);
}
/**
* @Route("/checkmail/{to}/{title}/{body}", name="check_mail")
*/
public function checkMail (\Swift_Mailer $mailer, string $to = "daniel@kowszun.waw.pl", string $title = "MAIL TEST", string $body = "Treść testowego maila"): Response {
$message = (new \Swift_Message($title))
->setFrom("daniel@kowszun.waw.pl", "INFORPOL NET")
->setTo($to, "Daniel Kowszun")
->setBody(
/*$this->renderView(
// templates/emails/registration.html.twig
'emails/registration.html.twig',
['name' => $name]
),*/
$body,
'text/html'
)
// you can remove the following code if you don't define a text version for your emails
/*->addPart(
$this->renderView(
// templates/emails/registration.txt.twig
'emails/registration.txt.twig',
['name' => $name]
),
'text/plain'
)*/
;
$mailer->send($message);
return new Response ("ODBIORCA: " . $to . "<br/>TEMAT: " . $title . "<br/>TREŚĆ: " . $body . "<br/>STATUS: OK");
}
}