Skip to content

Commit

Permalink
refactor(themes): Add type casts, reduce dup code
Browse files Browse the repository at this point in the history
  • Loading branch information
zooley committed Apr 1, 2024
1 parent a937b29 commit e3d85db
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 38 deletions.
41 changes: 28 additions & 13 deletions app/Modules/Themes/Entities/ThemeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ public function getScanPath(): string
*/
public function find(string $name): ?Theme
{
$lname = strtolower($name);

foreach ($this->allEnabled() as $theme)
{
if ($theme->getLowerElement() == strtolower($name))
if ($theme->getLowerElement() == $lname)
{
return $theme;
}
Expand All @@ -91,17 +93,19 @@ public function find(string $name): ?Theme
*/
public function findOrFail(string $name): ?Theme
{
$lname = strtolower($name);

foreach ($this->allEnabled() as $theme)
{
if ($theme->getLowerElement() == strtolower($name))
if ($theme->getLowerElement() == $lname)
{
return $theme;
}
}

foreach ($this->allDisabled() as $theme)
{
if ($theme->getLowerElement() == strtolower($name))
if ($theme->getLowerElement() == $lname)
{
return $theme;
}
Expand Down Expand Up @@ -184,7 +188,7 @@ public function allDisabled(): array
* @param string $type
* @return array<string,Theme>
*/
public function allByType($type = 'site')
public function allByType(string $type = 'site')
{
$themes = [];

Expand Down Expand Up @@ -273,7 +277,7 @@ private function getThemesFromFiles(int $state = null): Collection
*/
private function getThemesFromDatabase(int $state = null, string $type = null): Collection
{
$s = (new Model)->getTable();
/*$s = (new Model)->getTable();
$query = $this->getDatabase()
->table($s)
Expand All @@ -285,21 +289,32 @@ private function getThemesFromDatabase(int $state = null, string $type = null):
$s . '.params',
$s . '.protected',
$s . '.client_id'
]);*/

$query = Model::query()
->select([
'id',
'enabled',
'name',
'element',
'params',
'protected',
'client_id'
]);

if (!is_null($state))
{
$query->where($s . '.enabled', '=', $state);
$query->where('enabled', '=', $state);
}
if (!is_null($type))
{
$query->where($s . '.client_id', '=', $type == 'admin' ? 1 : 0);
$query->where('client_id', '=', $type == 'admin' ? 1 : 0);
}
$query
->where($s . '.type', '=', 'theme')
->orderBy($s . '.enabled', 'desc');

$rows = $query->get();
$rows = $query
->where('type', '=', 'theme')
->orderBy('enabled', 'desc')
->get();

if (count($rows) <= 0)
{
Expand Down Expand Up @@ -354,10 +369,10 @@ public function getFiles()
/**
* @return \Illuminate\Database\DatabaseManager
*/
protected function getDatabase()
/*protected function getDatabase()
{
return $this->app['db'];
}
}*/

/**
* @return \Illuminate\Config\Repository
Expand Down
9 changes: 6 additions & 3 deletions app/Modules/Themes/Models/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use App\Halcyon\Models\Casts\Params;
use App\Halcyon\Form\Form;
use Carbon\Carbon;
Expand All @@ -26,6 +27,8 @@
* @property int $ordering
* @property Carbon|null $updated_at
* @property int $updated_by
*
* @property string $api
*/
class Theme extends Model
{
Expand Down Expand Up @@ -130,7 +133,7 @@ public function duplicate(): bool
* @param string $name The name.
* @return string New name.
*/
protected function generateNewTitle($name): string
protected function generateNewTitle(string $name): string
{
// Alter the name
$style = self::query()
Expand Down Expand Up @@ -254,9 +257,9 @@ public function path(): string
* Get active templates for specified client
*
* @param int $client_id
* @return Theme|null
* @return Collection
*/
public function allActive($client_id = 0)
public function allActive(int $client_id = 0): Collection
{
return self::query()
->where('enabled', '=', 1)
Expand Down
4 changes: 2 additions & 2 deletions app/Modules/Themes/Publishing/AssetPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AssetPublisher extends Publisher
*
* @return string
*/
public function getDestinationPath()
public function getDestinationPath(): string
{
return $this->repository->getAssetPath($this->theme->getLowerName());
}
Expand All @@ -26,7 +26,7 @@ public function getDestinationPath()
*
* @return string
*/
public function getSourcePath()
public function getSourcePath(): string
{
return config('module.themes.paths.themes', app_path('Themes'));
}
Expand Down
34 changes: 18 additions & 16 deletions app/Modules/Themes/Publishing/Publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Modules\Themes\Publishing;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use App\Modules\Themes\Contracts\PublisherInterface;
//use App\Modules\Themes\Contracts\RepositoryInterface;
use App\Modules\Themes\Entities\Theme;
Expand All @@ -25,7 +26,7 @@ abstract class Publisher implements PublisherInterface
/**
* The laravel console instance.
*
* @var \Illuminate\Console\Command
* @var Command
*/
protected $console;

Expand Down Expand Up @@ -65,7 +66,7 @@ public function __construct(Theme $theme)
*
* @return self
*/
public function showMessage()
public function showMessage(): self
{
$this->showMessage = true;

Expand All @@ -77,7 +78,7 @@ public function showMessage()
*
* @return self
*/
public function hideMessage()
public function hideMessage(): self
{
$this->showMessage = false;

Expand All @@ -89,7 +90,7 @@ public function hideMessage()
*
* @return Theme
*/
public function getTheme()
public function getTheme(): self
{
return $this->theme;
}
Expand All @@ -99,7 +100,7 @@ public function getTheme()
* @param RepositoryInterface $repository
* @return $this
*/
public function setRepository($repository)//RepositoryInterface
public function setRepository(RepositoryInterface $repository): self
{
$this->repository = $repository;

Expand All @@ -111,19 +112,18 @@ public function setRepository($repository)//RepositoryInterface
*
* @return RepositoryInterface
*/
public function getRepository()
public function getRepository(): RepositoryInterface
{
return $this->repository;
}

/**
* Set console instance.
*
* @param \Illuminate\Console\Command $console
*
* @param Command $console
* @return $this
*/
public function setConsole(Command $console)
public function setConsole(Command $console): self
{
$this->console = $console;

Expand All @@ -133,19 +133,19 @@ public function setConsole(Command $console)
/**
* Get console instance.
*
* @return \Illuminate\Console\Command
* @return Command
*/
public function getConsole()
public function getConsole(): Command
{
return $this->console;
}

/**
* Get laravel filesystem instance.
*
* @return \Illuminate\Filesystem\Filesystem
* @return Filesystem
*/
public function getFilesystem()
public function getFilesystem(): Filesystem
{
return $this->repository->getFiles();
}
Expand All @@ -155,19 +155,21 @@ public function getFilesystem()
*
* @return string
*/
abstract public function getDestinationPath();
abstract public function getDestinationPath(): string;

/**
* Get source path.
*
* @return string
*/
abstract public function getSourcePath();
abstract public function getSourcePath(): string;

/**
* Publish something.
*
* @throws \RuntimeException
*/
public function publish()
public function publish(): void
{
if (!$this->console instanceof Command)
{
Expand Down
4 changes: 2 additions & 2 deletions app/Modules/Themes/Resources/views/admin/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
<div class="row">
<div class="col-md-9">
<div class="form-group">
<label for="field-name">{{ trans('themes::themes.title') }}: <span class="required">{{ trans('global.required') }}</span></label>
<label for="field-name" class="form-label">{{ trans('themes::themes.title') }}: <span class="required">{{ trans('global.required') }}</span></label>
<input type="text" name="fields[name]" id="field-name" class="form-control required" required maxlength="250" value="{{ $row->name }}" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label id="field-client_id">{{ trans('themes::themes.type') }}:</label>
<label id="field-client_id" class="form-label">{{ trans('themes::themes.type') }}:</label>
<select name="fields[client_id]" id="field-client_id" class="form-control">
<option value="0"<?php if ($row->client_id == '0'): echo ' selected="selected"'; endif;?>>{{ trans('themes::themes.site') }}</option>
<option value="1"<?php if ($row->client_id == '1'): echo ' selected="selected"'; endif;?>>{{ trans('themes::themes.admin') }}</option>
Expand Down
4 changes: 2 additions & 2 deletions app/Modules/Themes/Resources/views/admin/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@

<fieldset id="filter-bar" class="container-fluid">
<div class="row">
<div class="col col-md-4">
<div class="col col-md-3">
<label class="sr-only visually-hidden" for="filter_search">{{ trans('search.label') }}</label>
<span class="input-group">
<input type="search" enterkeyhint="search" name="search" id="filter_search" class="form-control filter" placeholder="{{ trans('search.placeholder') }}" value="{{ $filters['search'] }}" />
<span class="input-group-append"><button type="submit" class="input-group-text"><span class="fa fa-search" aria-hidden="true"></span><span class="sr-only visually-hidden">{{ trans('search.submit') }}</span></button></span>
</span>
</div>
<div class="col col-md-5">
<div class="col col-md-6">
</div>
<div class="col col-md-3">
<label class="sr-only visually-hidden" for="filter_client_id">{{ trans('themes::themes.type') }}</label>
Expand Down

0 comments on commit e3d85db

Please sign in to comment.