Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomano committed Dec 16, 2024
1 parent 641fd4d commit 3ba3f88
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 46 deletions.
45 changes: 45 additions & 0 deletions src/Sources/Optimus/Handlers/CoreHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

/**
* @package Optimus
* @link https://custom.simplemachines.org/mods/index.php?mod=2659
* @author Bugo https://dragomano.ru/mods/optimus
* @copyright 2010-2024 Bugo
* @license https://opensource.org/licenses/artistic-license-2.0 Artistic-2.0
*
* @version 3.0 RC1
*/

namespace Bugo\Optimus\Handlers;

use Bugo\Compat\IntegrationHook;
use Bugo\Compat\Lang;
use Bugo\Compat\Utils;
use Bugo\Optimus\Utils\Copyright;

if (! defined('SMF'))
die('No direct access...');

final class CoreHandler
{
public function __invoke(): void
{
IntegrationHook::add(
'integrate_load_theme', self::class . '::loadTheme#', false, __FILE__
);

IntegrationHook::add(
'integrate_credits', self::class . '::credits#', false, __FILE__
);
}

public function loadTheme(): void
{
Lang::load('Optimus/Optimus');
}

public function credits(): void
{
Utils::$context['credits_modifications'][] = Copyright::getLink();
}
}
1 change: 1 addition & 0 deletions src/Sources/Optimus/Handlers/HandlerLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
final class HandlerLoader
{
private array $handlers = [
CoreHandler::class,
SettingHandler::class,
BoardHandler::class,
TagHandler::class,
Expand Down
20 changes: 0 additions & 20 deletions src/Sources/Optimus/Prime.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@

namespace Bugo\Optimus;

use Bugo\Compat\{IntegrationHook, Lang, Utils};
use Bugo\Optimus\Addons\AddonInterface;
use Bugo\Optimus\Events\AddonEvent;
use Bugo\Optimus\Events\DispatcherFactory;
use Bugo\Optimus\Handlers\HandlerLoader;
use Bugo\Optimus\Utils\Copyright;

if (! defined('SMF'))
die('No direct access...');
Expand All @@ -31,24 +29,6 @@ public function __construct()

public function __invoke(): void
{
IntegrationHook::add(
'integrate_load_theme', self::class . '::loadTheme#', false, __FILE__
);

IntegrationHook::add(
'integrate_credits', self::class . '::credits#', false, __FILE__
);

(new DispatcherFactory())()->dispatch(new AddonEvent(AddonInterface::HOOK_EVENT, $this));
}

public function loadTheme(): void
{
Lang::load('Optimus/Optimus');
}

public function credits(): void
{
Utils::$context['credits_modifications'][] = Copyright::getLink() . Copyright::getYears();
}
}
4 changes: 2 additions & 2 deletions src/Sources/Optimus/Utils/Copyright.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public static function getLink(): string
->setAttribute('target', '_blank')
->setAttribute('rel', 'noopener')
->setAttribute('title', OP_VERSION)
->toHtml();
->toHtml() . self::getYears();
}

public static function getYears(): string
protected static function getYears(): string
{
return ' &copy; 2010&ndash;' . date('Y') . ', Bugo';
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Handlers/CoreHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

use Bugo\Compat\Lang;
use Bugo\Compat\Utils;
use Bugo\Optimus\Handlers\CoreHandler;

beforeEach(function () {
$this->handler = new CoreHandler();
});

it('loads languages', function () {
$this->handler->loadTheme();

expect(Lang::$txt['optimus_title'])
->toEqual('Search Engine Optimization');
});

it('adds copyright', function () {
$this->handler->credits();

expect(Utils::$context['credits_modifications'])
->not->toBeEmpty();
});
18 changes: 1 addition & 17 deletions tests/PrimeTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
<?php declare(strict_types=1);

use Bugo\Compat\Lang;
use Bugo\Compat\Utils;
use Bugo\Optimus\Prime;

beforeEach(function () {
$this->prime = new Prime();
$this->prime = new Prime();
});

it('runs __invoke', function () {
expect((new Prime())())->toBeNull();
});

it('loads languages', function () {
$this->prime->loadTheme();

expect(Lang::$txt['optimus_title'])
->toEqual('Search Engine Optimization');
});

it('adds copyright', function () {
$this->prime->credits();

expect(Utils::$context['credits_modifications'])
->not->toBeEmpty();
});
9 changes: 2 additions & 7 deletions tests/Utils/CopyrightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@

$link = Copyright::getLink();

expect($link)->toContain('https://dragomano.ru/mods/optimus');
expect($link)->toContain('https://dragomano.ru/mods/optimus')
->and($link)->toContain(' &copy; 2010&ndash;' . date('Y') . ', Bugo');

unset(Lang::$txt['lang_dictionary']);
});

it('gets years', function () {
$years = Copyright::getYears();

expect($years)->toEqual(' &copy; 2010&ndash;' . date('Y') . ', Bugo');
});

0 comments on commit 3ba3f88

Please sign in to comment.