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 @@ -35,22 +35,11 @@ class ArticleController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
return $this->render('default/article/info.html.twig', [
'title' => 'test'
@ -76,22 +65,11 @@ class ArticleController extends AbstractController @@ -76,22 +65,11 @@ class ArticleController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -270,4 +248,31 @@ class ArticleController extends AbstractController @@ -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 @@ -32,22 +32,11 @@ class SearchController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
$article = $request->query->get('article') ? (int) $request->query->get('article') : 1;
@ -160,4 +149,31 @@ class SearchController extends AbstractController @@ -160,4 +149,31 @@ class SearchController extends AbstractController
'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 @@ -37,22 +37,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -198,22 +187,11 @@ class TorrentController extends AbstractController @@ -198,22 +187,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -372,22 +350,11 @@ class TorrentController extends AbstractController @@ -372,22 +350,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -578,22 +545,11 @@ class TorrentController extends AbstractController @@ -578,22 +545,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -675,22 +631,11 @@ class TorrentController extends AbstractController @@ -675,22 +631,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -765,22 +710,11 @@ class TorrentController extends AbstractController @@ -765,22 +710,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -941,22 +875,11 @@ class TorrentController extends AbstractController @@ -941,22 +875,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -1038,22 +961,11 @@ class TorrentController extends AbstractController @@ -1038,22 +961,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -1122,22 +1034,11 @@ class TorrentController extends AbstractController @@ -1122,22 +1034,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -1211,22 +1112,11 @@ class TorrentController extends AbstractController @@ -1211,22 +1112,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -1327,22 +1217,11 @@ class TorrentController extends AbstractController @@ -1327,22 +1217,11 @@ class TorrentController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -1414,4 +1293,31 @@ class TorrentController extends AbstractController @@ -1414,4 +1293,31 @@ class TorrentController extends AbstractController
// Render response
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 @@ -24,22 +24,11 @@ class UserController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
return $this->redirectToRoute(
'user_dashboard',
@ -55,10 +44,16 @@ class UserController extends AbstractController @@ -55,10 +44,16 @@ class UserController extends AbstractController
)]
public function index(
Request $request,
ActivityService $activityService,
UserService $userService
UserService $userService,
ActivityService $activityService
): Response
{
$user = $this->initUser(
$request,
$userService,
$activityService
);
return $this->render(
'default/user/dashboard.html.twig',
[
@ -84,22 +79,11 @@ class UserController extends AbstractController @@ -84,22 +79,11 @@ class UserController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Process post request
if ($request->isMethod('post'))
@ -198,22 +182,11 @@ class UserController extends AbstractController @@ -198,22 +182,11 @@ class UserController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -284,22 +257,11 @@ class UserController extends AbstractController @@ -284,22 +257,11 @@ class UserController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isStatus())
{
@ -371,22 +333,11 @@ class UserController extends AbstractController @@ -371,22 +333,11 @@ class UserController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isModerator())
{
@ -456,22 +407,11 @@ class UserController extends AbstractController @@ -456,22 +407,11 @@ class UserController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isModerator())
{
@ -543,22 +483,11 @@ class UserController extends AbstractController @@ -543,22 +483,11 @@ class UserController extends AbstractController
): Response
{
// 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()
);
}
$user = $this->initUser(
$request,
$userService,
$activityService
);
if (!$user->isModerator())
{
@ -643,4 +572,31 @@ class UserController extends AbstractController @@ -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