diff --git a/src/Skobkin/Bundle/PointToolsBundle/Command/UpdatePostsCommand.php b/src/Skobkin/Bundle/PointToolsBundle/Command/UpdatePostsCommand.php new file mode 100644 index 0000000..46674a5 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Command/UpdatePostsCommand.php @@ -0,0 +1,47 @@ +setName('point:update:posts') + ->setDescription('Update posts from /all') + ->addOption( + 'check-only', + null, + InputOption::VALUE_NONE, + 'If set, command will not perform write operations in the database' + ) + // @todo add option for checking only selected user + ; + } + + /** + * @param Input $input + * @param Output $output + * @return bool + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $log = $this->getContainer()->get('logger'); + + $log->info('UpdateSubscriptionsCommand started.'); + + /** @var UserApi $api */ + $api = $this->getContainer()->get('skobkin_point_tools.api_user'); + + + } +} \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php new file mode 100644 index 0000000..1c97c1b --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php @@ -0,0 +1,140 @@ +em = $entityManager; + } + + public function getName() + { + return 'skobkin_point_tools_api_post'; + } + + /** + * Get user subscribers by user id + * + * @param int $id + * @return User[] + */ + public function getUserSubscribersById($id) + { + if (!is_numeric($id)) { + throw new \InvalidArgumentException('$id must be an integer'); + } + + $usersList = $this->getGetRequestData('/api/user/id/' . (int) $id . '/subscribers', [], true); + + $users = $this->getUsersFromList($usersList); + + return $users; + } + + /** + * @param array $users + * @return User[] + */ + private function getUsersFromList(array $users = []) + { + /** @var EntityRepository $userRepo */ + $userRepo = $this->em->getRepository('SkobkinPointToolsBundle:User'); + + $resultUsers = []; + + foreach ($users as $userData) { + if (array_key_exists('id', $userData) && array_key_exists('login', $userData) && array_key_exists('name', $userData) && is_numeric($userData['id'])) { + + // @todo Optimize with prehashed id's list + $user = $userRepo->findOneBy(['id' => $userData['id']]); + + if (!$user) { + $user = new User(); + $user->setId((int) $userData['id']); + $this->em->persist($user); + } + + // Updating data + if ($user->getLogin() !== $userData['login']) { + $user->setLogin($userData['login']); + } + if ($user->getName() !== $userData['name']) { + $user->setName($userData['name']); + } + + $resultUsers[] = $user; + } + } + + $this->em->flush(); + + return $resultUsers; + } + + /** + * @param array $posts + */ + private function getPostsFromList(array $posts = []) + { + /** @var EntityRepository $postRepo */ + $postRepo = $this->em->getRepository('SkobkinPointToolsBundle:Blogs\Post'); + + $resultUsers = []; + + foreach ($posts as $postData) { + if (array_key_exists('id', $postData) && array_key_exists('uid', $postData) && array_key_exists('post', $postData)) { + + // @todo Optimize with prehashed id's list + $post = $postRepo->findOneBy(['id' => $postData['id']]); + + if (!$post) { + $post = new Post(); + $post + ->setId($postData['id']) + ->setText($postData['post']['text']) + //->setCreatedAt() + //->setAuthor() + ->setType($postData['post']['type']) + ; + $this->em->persist($post); + } + + // Updating data + if ($post->getLogin() !== $postData['login']) { + $post->setLogin($postData['login']); + } + if ($post->getName() !== $postData['name']) { + $post->setName($postData['name']); + } + + $resultUsers[] = $post; + } + } + + $this->em->flush(); + + return $resultUsers; + } +} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php index 5de211f..8a05463 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php @@ -13,10 +13,6 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User; */ class UserApi extends AbstractApi { - const PATH_USER_INFO = '/api/user/%s'; - const PATH_USER_SUBSCRIPTIONS = '/api/user/%s/subscriptions'; - const PATH_USER_SUBSCRIBERS = '/api/user/%s/subscribers'; - const AVATAR_SIZE_SMALL = '24'; const AVATAR_SIZE_MEDIUM = '40'; const AVATAR_SIZE_LARGE = '80'; @@ -83,10 +79,6 @@ class UserApi extends AbstractApi */ private function getUsersFromList(array $users = []) { - if (!is_array($users)) { - throw new \InvalidArgumentException('$users must be an array'); - } - /** @var EntityRepository $userRepo */ $userRepo = $this->em->getRepository('SkobkinPointToolsBundle:User'); @@ -96,7 +88,7 @@ class UserApi extends AbstractApi if (array_key_exists('id', $userData) && array_key_exists('login', $userData) && array_key_exists('name', $userData) && is_numeric($userData['id'])) { // @todo Optimize with prehashed id's list - $user = $userRepo->findOneBy(['id' => $userData['id']]); + $user = $userRepo->find($userData['id']); if (!$user) { $user = new User();