2021-10-30 12:16:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Zoomyboy\Social\Classes;
|
|
|
|
|
|
|
|
use Event;
|
2021-10-30 20:44:11 +02:00
|
|
|
use Generator;
|
2021-10-30 12:16:27 +02:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Media\Classes\MediaLibrary;
|
|
|
|
use Zoomyboy\Social\Models\Page;
|
2021-10-30 13:32:09 +02:00
|
|
|
use Zoomyboy\Social\Models\Setting;
|
2021-10-30 12:16:27 +02:00
|
|
|
|
|
|
|
abstract class SocialService {
|
|
|
|
|
|
|
|
protected $page;
|
|
|
|
protected $media;
|
|
|
|
|
|
|
|
abstract public function getType(): string;
|
|
|
|
|
2021-10-30 20:44:11 +02:00
|
|
|
abstract public function posts(): Generator;
|
|
|
|
|
2021-10-30 12:16:27 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->media = MediaLibrary::instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setPage(Page $page): self
|
|
|
|
{
|
|
|
|
$this->page = $page;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearAll(): void
|
|
|
|
{
|
|
|
|
foreach ($this->pages() as $page) {
|
|
|
|
$this->setPage($page)->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function syncAll(): void
|
|
|
|
{
|
|
|
|
foreach ($this->pages() as $page) {
|
|
|
|
$this->setPage($page)->sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:32:09 +02:00
|
|
|
public function pages(): Collection
|
2021-10-30 12:16:27 +02:00
|
|
|
{
|
|
|
|
return Page::where('type', $this->getType())->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clear() {
|
|
|
|
$this->page->delete();
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:32:09 +02:00
|
|
|
public function cleanOutdated(): void
|
|
|
|
{
|
|
|
|
if (!is_numeric(Setting::get('max_posts'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ($this->page->posts()->count() > 0 && $this->page->posts()->count() > Setting::get('max_posts')) {
|
|
|
|
$this->page->posts()->orderBy('created_at')->first()->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 12:16:27 +02:00
|
|
|
public function saveUrl(string $source, ?string $filename = null): string
|
|
|
|
{
|
|
|
|
$filename = $filename ?: pathinfo(parse_url($source, PHP_URL_PATH), PATHINFO_BASENAME);
|
|
|
|
$file = $this->page->mediaPath.$filename;
|
|
|
|
|
|
|
|
if (!$this->media->exists($file)) {
|
|
|
|
$this->media->put($file, file_get_contents($source));
|
|
|
|
Event::fire('media.file.upload', [null, $file, null]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2021-10-30 20:44:11 +02:00
|
|
|
protected function sync()
|
|
|
|
{
|
|
|
|
foreach ($this->posts() as $post) {
|
|
|
|
$existing = $this->page->posts()->where('remote_id', $post['remote_id'])->first();
|
|
|
|
|
|
|
|
if ($existing) {
|
|
|
|
$existing->update($post);
|
|
|
|
} else {
|
|
|
|
$existing = $this->page->posts()->create($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($post['attachments'] as $attachment) {
|
|
|
|
$existing->attachments()->updateOrCreate(['remote_id' => $attachment['remote_id']], $attachment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cleanOutdated();
|
|
|
|
}
|
|
|
|
|
2021-10-30 12:16:27 +02:00
|
|
|
}
|