-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserDataController.php
86 lines (74 loc) · 2.55 KB
/
UserDataController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\User;
use App\Service\Cache;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Twig\Environment;
class UserDataController extends AbstractController
{
private EntityManagerInterface $em;
private Environment $twig;
private Cache $cache;
public function __construct(
EntityManagerInterface $em,
Environment $twig,
Cache $cache
) {
$this->em = $em;
$this->twig = $twig;
$this->cache = $cache;
}
#[Route('/osoba,{user<\d+>?0}', name: 'user_data')]
public function userDataAction(
int $user,
string $cacheList,
int $cacheTime,
int $userLimit
): Response {
$cacheList = str_replace('{user}', (string) $user, $cacheList);
$cacheFile = '../templates/' . $cacheList;
$ur = $this->em->getRepository(User::class);
if (!$ur->isUserData($user)) {
throw $this->createNotFoundException('No user data found');
}
if (
!file_exists($cacheFile)
|| filemtime($cacheFile) <= time() - $cacheTime
) {
$randomUserList = $ur->getRandomUserList($userLimit);
$content = $this->twig->render(
'user_data/_user_data_list.html.twig',
['randomUserList' => $randomUserList]
);
$this->cache->cachePage($cacheFile, $content);
}
$ur->updateUserNumber($user);
$ur->updateUserRanking($user);
$userData = $ur->getUserData($user);
$this->em->refresh($userData);
$title = (
!empty($userData->getName()) && !empty($userData->getSurname())
) ? $userData->getName() . ' ' . $userData->getSurname() . (
(
$userData->getProvince() !== null
|| $userData->getCity() !== null
) ? ' -' . (
($userData->getCity() !== null) ? ' '
. $userData->getCity()->getName() : ''
) . (
($userData->getProvince() !== null) ? ' '
. $userData->getProvince()->getName() : ''
) : ''
) : '';
return $this->render('user_data/user_data.html.twig', [
'activeMenu' => 'user_data',
'cacheList' => $cacheList,
'userData' => $userData,
'title' => $title
]);
}
}