Skip to content

Commit

Permalink
Upgrade code to fully support PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenth committed Jul 6, 2021
1 parent 4518aaf commit 9f55ea5
Show file tree
Hide file tree
Showing 27 changed files with 124 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ 8.0, 7.4, 7.3 ]
php: [ 8.0, 7.4 ]
stability: [ prefer-lowest, prefer-stable ]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# 3.0.0

- Drop support for PHP 7.4 (minimum required PHP version 7.4).

# 2.2.1

- Fix error when performing a migration rollback.

# 2.2.0

- Update plugin dependencies (minimum required PHP version 7.3)
- Update plugin dependencies (minimum required PHP version 7.3).

# 2.1.0

Expand Down
23 changes: 1 addition & 22 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

class Plugin extends PluginBase
{
/**
* {@inheritDoc}
*/
public function pluginDetails(): array
{
return [
Expand All @@ -23,17 +20,11 @@ public function pluginDetails(): array
];
}

/**
* {@inheritDoc}
*/
public function register(): void
{
$this->registerConsoleCommand('Vdlp.RssFetcher', Commands\FetchRssCommand::class);
}

/**
* {@inheritDoc}
*/
public function registerComponents(): array
{
return [
Expand All @@ -43,22 +34,16 @@ public function registerComponents(): array
];
}

/**
* {@inheritDoc}
*/
public function registerReportWidgets(): array
{
return [
ReportWidgets\Headlines::class => [
'label' => 'RSS Headlines',
'code' => 'headlines',
]
],
];
}

/**
* {@inheritDoc}
*/
public function registerPermissions(): array
{
return [
Expand All @@ -81,9 +66,6 @@ public function registerPermissions(): array
];
}

/**
* {@inheritDoc}
*/
public function registerNavigation(): array
{
/** @var BackendHelper $backendHelper */
Expand Down Expand Up @@ -120,9 +102,6 @@ public function registerNavigation(): array
];
}

/**
* {@inheritDoc}
*/
public function registerFormWidgets(): array
{
return [
Expand Down
33 changes: 12 additions & 21 deletions classes/RssFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace Vdlp\RssFetcher\Classes;

use Carbon\Carbon;
use Exception;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Collection;
use Laminas\Feed\Reader\Entry\EntryInterface;
use Laminas\Feed\Reader\Entry\Rss;
use Laminas\Feed\Reader\Feed\FeedInterface;
use Laminas\Feed\Reader\Reader;
use October\Rain\Support\Facades\Event;
use October\Rain\Support\Traits\Singleton;
use Psr\Log\LoggerInterface;
use stdClass;
Expand All @@ -23,26 +22,18 @@ final class RssFetcher
{
use Singleton;

/**
* @var LoggerInterface
*/
private $log;

/**
* @var Dispatcher
*/
private $dispatcher;
private LoggerInterface $log;

protected function init(): void
{
$this->log = resolve(LoggerInterface::class);
$this->dispatcher = resolve(Dispatcher::class);
}

public function fetch(int $sourceId = null): void
public function fetch(?int $sourceId = null): void
{
$sources = $this->getSourceCollection($sourceId);
$sources->each(function (Source $source) {

$sources->each(function (Source $source): void {
try {
$this->fetchSource($source);
} catch (Throwable $e) {
Expand All @@ -65,13 +56,13 @@ private function fetchSource(Source $source): void
$dateCreated = $item->getDateCreated();

$title = $item->getTitle();
$this->dispatcher->fire('vdlp.rssfetcher.item.processTitle', [&$title]);
Event::dispatch('vdlp.rssfetcher.item.processTitle', [&$title]);

$content = $item->getContent();
$this->dispatcher->fire('vdlp.rssfetcher.item.processContent', [&$content]);
Event::dispatch('vdlp.rssfetcher.item.processContent', [&$content]);

$link = $item->getLink();
$this->dispatcher->fire('vdlp.rssfetcher.item.processLink', [&$link]);
Event::dispatch('vdlp.rssfetcher.item.processLink', [&$link]);

$attributes = [
'item_id' => $item->getId(),
Expand All @@ -81,7 +72,7 @@ private function fetchSource(Source $source): void
'description' => $content,
'category' => implode(', ', $item->getCategories()->getValues()),
'comments' => $item->getCommentLink(),
'pub_date' => $dateCreated !== null ? $item->getDateCreated()->format('Y-m-d H:i:s') : null,
'pub_date' => $dateCreated !== null ? $dateCreated->format('Y-m-d H:i:s') : null,
'is_published' => (bool) $source->getAttribute('publish_new_items'),
'author' => $this->getAuthor($channel, $item),
];
Expand Down Expand Up @@ -120,7 +111,7 @@ private function getAuthor(FeedInterface $feed, EntryInterface $entry): ?string
$result = null;
$author = $entry->getAuthor();

if ($author === null || empty($author)) {
if ($author === null || count($author) === 0) {
$author = $feed->getAuthor();
}

Expand All @@ -133,7 +124,7 @@ private function getAuthor(FeedInterface $feed, EntryInterface $entry): ?string
return $result;
}

private function getSourceCollection(int $sourceId = null): Collection
private function getSourceCollection(?int $sourceId = null): Collection
{
$sources = new Collection();

Expand All @@ -143,7 +134,7 @@ private function getSourceCollection(int $sourceId = null): Collection
->where('is_enabled', '=', true)
->first();

if ($source) {
if ($source !== null) {
$sources = new Collection([$source]);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion commands/FetchRssCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Vdlp\RssFetcher\Classes\RssFetcher;

class FetchRssCommand extends Command
final class FetchRssCommand extends Command
{
public function __construct()
{
Expand Down
9 changes: 3 additions & 6 deletions components/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
use Throwable;
use Vdlp\RssFetcher\Models\Item;

class Items extends ComponentBase
final class Items extends ComponentBase
{
/**
* @var Collection
*/
public $items;
public ?Collection $items = null;

public function componentDetails(): array
{
Expand Down Expand Up @@ -45,7 +42,7 @@ public function onRun(): void
$sourceId = (int) $this->property('sourceId');

$this->items = self::loadItems(
(int) $this->property('maxItems', 10),
(int) $this->property('maxItems', '10'),
$sourceId > 0 ? $sourceId : null
);
}
Expand Down
10 changes: 4 additions & 6 deletions components/PaginatableItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
namespace Vdlp\RssFetcher\Components;

use Cms\Classes\ComponentBase;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Throwable;
use Vdlp\RssFetcher\Models\Item;

class PaginatableItems extends ComponentBase
final class PaginatableItems extends ComponentBase
{
/**
* @var LengthAwarePaginator
*/
public $items;
public ?Paginator $items = null;

public function componentDetails(): array
{
Expand Down Expand Up @@ -42,7 +40,7 @@ public function onRun(): void
$this->items = $this->loadItems();
}

protected function loadItems(): LengthAwarePaginator
private function loadItems(): Paginator
{
try {
$items = Item::query()
Expand Down
7 changes: 2 additions & 5 deletions components/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
use Throwable;
use Vdlp\RssFetcher\Models\Source;

class Sources extends ComponentBase
final class Sources extends ComponentBase
{
/**
* @var Collection
*/
public $sources;
public ?Collection $sources = null;

public function componentDetails(): array
{
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "^7.3 || ^8.0",
"php": "^7.4 || ^8.0",
"laminas/laminas-feed": "^2.14",
"laminas/laminas-http": "^2.14",
"composer/installers": "^1.0"
Expand Down
6 changes: 3 additions & 3 deletions controllers/Feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* @mixin FormController
* @mixin ListController
*/
class Feeds extends Controller
final class Feeds extends Controller
{
public $implement = [
FormController::class,
ListController::class,
];

public $formConfig = 'config_form.yaml';
public $listConfig = 'config_list.yaml';
public string $formConfig = 'config_form.yaml';
public string $listConfig = 'config_list.yaml';
protected $requiredPermissions = ['vdlp.rssfetcher.access_feeds'];

public function __construct()
Expand Down
43 changes: 22 additions & 21 deletions controllers/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* @mixin FormController
* @mixin ListController
*/
class Items extends Controller
final class Items extends Controller
{
public $implement = [
FormController::class,
ListController::class,
];

public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public string $listConfig = 'config_list.yaml';
public string $formConfig = 'config_form.yaml';
protected $requiredPermissions = ['vdlp.rssfetcher.access_items'];

public function __construct()
Expand All @@ -32,52 +32,53 @@ public function __construct()
NavigationManager::instance()->setContext('Vdlp.RssFetcher', 'rssfetcher', 'items');
}

// @codingStandardsIgnoreStart

public function index_onDelete(): array
public function onDelete(): array
{
foreach ($this->getCheckedIds() as $sourceId) {
if (!$source = Item::query()->find($sourceId)) {
foreach ($this->getCheckedIds() as $itemId) {
/** @var ?Item $item */
$item = Item::query()->find($itemId);

if ($item === null) {
continue;
}

$source->delete();
$item->delete();
}

return $this->listRefresh();
}

public function index_onPublish(): array
public function onPublish(): array
{
return $this->publishItem(true);
}

public function index_onUnpublish(): array
public function onUnpublish(): array
{
return $this->publishItem(false);
}

// @codingStandardsIgnoreEnd

private function publishItem($publish): array
private function publishItem(bool $publish): array
{
foreach ($this->getCheckedIds() as $sourceId) {
if (!$source = Item::query()->find($sourceId)) {
foreach ($this->getCheckedIds() as $itemId) {
/** @var ?Item $item */
$item = Item::query()->find($itemId);

if ($item === null) {
continue;
}

$source->update(['is_published' => $publish]);
$item->update(['is_published' => $publish]);
}

return $this->listRefresh();
}

private function getCheckedIds(): array
{
if (($checkedIds = post('checked'))
&& is_array($checkedIds)
&& count($checkedIds)
) {
$checkedIds = post('checked');

if (is_array($checkedIds) && count($checkedIds) > 0) {
return $checkedIds;
}

Expand Down
Loading

0 comments on commit 9f55ea5

Please sign in to comment.