Browse Source

make common initUser method

main
ghost 1 year ago
parent
commit
c1c5c7fa59
  1. 69
      src/Controller/ArticleController.php
  2. 48
      src/Controller/SearchController.php
  3. 258
      src/Controller/TorrentController.php
  4. 184
      src/Controller/UserController.php

69
src/Controller/ArticleController.php

@ -35,22 +35,11 @@ class ArticleController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
return $this->render('default/article/info.html.twig', [ return $this->render('default/article/info.html.twig', [
'title' => 'test' 'title' => 'test'
@ -76,22 +65,11 @@ class ArticleController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -270,4 +248,31 @@ class ArticleController extends AbstractController
] ]
); );
} }
private function initUser(
Request $request,
UserService $userService,
ActivityService $activityService
): ?\App\Entity\User
{
// Init user
if (!$user = $userService->findUserByAddress($request->getClientIp()))
{
$user = $userService->addUser(
$request->getClientIp(),
time(),
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
return $user;
}
} }

48
src/Controller/SearchController.php

@ -32,22 +32,11 @@ class SearchController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
$article = $request->query->get('article') ? (int) $request->query->get('article') : 1; $article = $request->query->get('article') ? (int) $request->query->get('article') : 1;
@ -160,4 +149,31 @@ class SearchController extends AbstractController
'type' => $type, 'type' => $type,
]); ]);
} }
private function initUser(
Request $request,
UserService $userService,
ActivityService $activityService
): ?\App\Entity\User
{
// Init user
if (!$user = $userService->findUserByAddress($request->getClientIp()))
{
$user = $userService->addUser(
$request->getClientIp(),
time(),
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
return $user;
}
} }

258
src/Controller/TorrentController.php

@ -37,22 +37,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
// Init torrent // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -198,22 +187,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -372,22 +350,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -578,22 +545,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
// Init torrent // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -675,22 +631,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
// Init torrent // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -765,22 +710,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -941,22 +875,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
// Init torrent // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -1038,22 +961,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
// Init torrent // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -1122,22 +1034,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -1211,22 +1112,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -1327,22 +1217,11 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -1414,4 +1293,31 @@ class TorrentController extends AbstractController
// Render response // Render response
return new Response(); // @TODO return new Response(); // @TODO
} }
private function initUser(
Request $request,
UserService $userService,
ActivityService $activityService
): ?\App\Entity\User
{
// Init user
if (!$user = $userService->findUserByAddress($request->getClientIp()))
{
$user = $userService->addUser(
$request->getClientIp(),
time(),
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
return $user;
}
} }

184
src/Controller/UserController.php

@ -24,22 +24,11 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
return $this->redirectToRoute( return $this->redirectToRoute(
'user_dashboard', 'user_dashboard',
@ -55,10 +44,16 @@ class UserController extends AbstractController
)] )]
public function index( public function index(
Request $request, Request $request,
ActivityService $activityService, UserService $userService,
UserService $userService ActivityService $activityService
): Response ): Response
{ {
$user = $this->initUser(
$request,
$userService,
$activityService
);
return $this->render( return $this->render(
'default/user/dashboard.html.twig', 'default/user/dashboard.html.twig',
[ [
@ -84,22 +79,11 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
// Process post request // Process post request
if ($request->isMethod('post')) if ($request->isMethod('post'))
@ -198,22 +182,11 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -284,22 +257,11 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus()) if (!$user->isStatus())
{ {
@ -371,22 +333,11 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isModerator()) if (!$user->isModerator())
{ {
@ -456,22 +407,11 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isModerator()) if (!$user->isModerator())
{ {
@ -543,22 +483,11 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
if (!$user = $userService->findUserByAddress($request->getClientIp())) $user = $this->initUser(
{ $request,
$user = $userService->addUser( $userService,
$request->getClientIp(), $activityService
time(), );
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isModerator()) if (!$user->isModerator())
{ {
@ -643,4 +572,31 @@ class UserController extends AbstractController
] ]
); );
} }
private function initUser(
Request $request,
UserService $userService,
ActivityService $activityService
): ?\App\Entity\User
{
// Init user
if (!$user = $userService->findUserByAddress($request->getClientIp()))
{
$user = $userService->addUser(
$request->getClientIp(),
time(),
$this->getParameter('app.locale'),
explode('|', $this->getParameter('app.locales')),
$this->getParameter('app.theme')
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
return $user;
}
} }
Loading…
Cancel
Save