oc-social-plugin/classes/SocialService.php

66 lines
1.4 KiB
PHP

<?php
namespace Zoomyboy\Social\Classes;
use Event;
use Illuminate\Support\Collection;
use Media\Classes\MediaLibrary;
use Zoomyboy\Social\Models\Page;
abstract class SocialService {
protected $page;
protected $media;
abstract public function getType(): string;
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();
}
}
protected function pages(): Collection
{
return Page::where('type', $this->getType())->get();
}
public function clear() {
$this->page->delete();
}
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;
}
}