Browse Source

make common initUser method

main
ghost 1 year ago
parent
commit
c1c5c7fa59
  1. 65
      src/Controller/ArticleController.php
  2. 46
      src/Controller/SearchController.php
  3. 236
      src/Controller/TorrentController.php
  4. 170
      src/Controller/UserController.php

65
src/Controller/ArticleController.php

@ -35,23 +35,12 @@ class ArticleController extends AbstractController @@ -35,23 +35,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
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;
}
}

46
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;
}
}

236
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,23 +631,12 @@ class TorrentController extends AbstractController @@ -675,23 +631,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
@ -765,23 +710,12 @@ class TorrentController extends AbstractController @@ -765,23 +710,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus())
{
// @TODO
@ -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,23 +961,12 @@ class TorrentController extends AbstractController @@ -1038,23 +961,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
@ -1122,23 +1034,12 @@ class TorrentController extends AbstractController @@ -1122,23 +1034,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus())
{
// @TODO
@ -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;
}
}

170
src/Controller/UserController.php

@ -24,23 +24,12 @@ class UserController extends AbstractController @@ -24,23 +24,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
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,23 +182,12 @@ class UserController extends AbstractController @@ -198,23 +182,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isStatus())
{
// @TODO
@ -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,23 +333,12 @@ class UserController extends AbstractController @@ -371,23 +333,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isModerator())
{
// @TODO
@ -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,23 +483,12 @@ class UserController extends AbstractController @@ -543,23 +483,12 @@ 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')
$user = $this->initUser(
$request,
$userService,
$activityService
);
// Add user join event
$activityService->addEventUserAdd(
$user->getId(),
time()
);
}
if (!$user->isModerator())
{
// @TODO
@ -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