61 lines
1.4 KiB
PHP
61 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Library\VK\Service;
|
|
|
|
use App\Library\VK\Entity\VkPost;
|
|
use App\Library\VK\Mapper\PostMapper;
|
|
use App\Library\VK\Resources\GroupResource;
|
|
use App\Library\VK\Resources\WallResource;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Generator;
|
|
|
|
class VkPostImportService
|
|
{
|
|
public function __construct(
|
|
private WallResource $wall,
|
|
private GroupResource $group,
|
|
private PostMapper $mapper
|
|
) {
|
|
|
|
}
|
|
|
|
public function run(): void
|
|
{
|
|
$group = $this->getGroupInfo('ledstarband');
|
|
$posts = $this->getWallPosts('ledstarband', 10);
|
|
|
|
foreach($posts as $item) {
|
|
$post = $this->mapper->map($item);
|
|
$post->setSourceName($group['name']);
|
|
|
|
if(! $post->isEmpty()) {
|
|
print_r($post->toArray());
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/** @return Generator<int, array> */
|
|
private function getWallPosts(string $id, int $count = 5): Generator
|
|
{
|
|
$result = $this->wall->get($id, $count);
|
|
|
|
if (empty ($result['items']) ) {
|
|
Log::info('Post import failed: no items found');
|
|
}
|
|
|
|
# echo "\nFound items: ".$result['count']."\n";
|
|
foreach ($result['items'] as $item) {
|
|
yield $item;
|
|
}
|
|
}
|
|
|
|
private function getGroupInfo(string $groupId): ?array
|
|
{
|
|
$res = $this->group->getById($groupId);
|
|
return $res[0] ?? null;
|
|
}
|
|
}
|