Skip to content

Commit

Permalink
Issue #309 Replace Facade usages with actual services
Browse files Browse the repository at this point in the history
  • Loading branch information
stevanpavlovic authored and ifox committed Jul 22, 2019
1 parent 6b5c49a commit a6106b7
Show file tree
Hide file tree
Showing 24 changed files with 406 additions and 122 deletions.
33 changes: 24 additions & 9 deletions src/Commands/Build.php
Expand Up @@ -2,8 +2,8 @@

namespace A17\Twill\Commands;

use Illuminate\Support\Facades\File;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

class Build extends Command
Expand All @@ -23,6 +23,21 @@ class Build extends Command
protected $description = "Build Twill assets (experimental)";

/**
* @var Filesystem
*/
protected $filesystem;

/**
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
parent::__construct();

$this->filesystem = $filesystem;
}

/*
* Executes the console command.
*
* @return mixed
Expand Down Expand Up @@ -53,9 +68,9 @@ public function handle()
$progressBar->setMessage("Copying assets...");
$progressBar->advance();

File::copyDirectory(base_path('vendor/area17/twill/public'), public_path());
$this->filesystem->copyDirectory(base_path('vendor/area17/twill/public'), public_path());

File::delete(public_path('hot'));
$this->filesystem->delete(public_path('hot'));

$this->info('');
$progressBar->setMessage("Done.");
Expand Down Expand Up @@ -90,16 +105,16 @@ private function copyBlocks()
$localCustomBlocksPath = resource_path('assets/js/blocks');
$twillCustomBlocksPath = base_path('vendor/area17/twill/frontend/js/components/blocks/customs');

if (!File::exists($twillCustomBlocksPath)) {
File::makeDirectory($twillCustomBlocksPath);
if (!$this->filesystem->exists($twillCustomBlocksPath)) {
$this->filesystem->makeDirectory($twillCustomBlocksPath);
}

File::cleanDirectory($twillCustomBlocksPath);
$this->filesystem->cleanDirectory($twillCustomBlocksPath);

if (!File::exists($localCustomBlocksPath)) {
File::makeDirectory($localCustomBlocksPath);
if (!$this->filesystem->exists($localCustomBlocksPath)) {
$this->filesystem->makeDirectory($localCustomBlocksPath);
}

File::copyDirectory($localCustomBlocksPath, $twillCustomBlocksPath);
$this->filesystem->copyDirectory($localCustomBlocksPath, $twillCustomBlocksPath);
}
}
22 changes: 19 additions & 3 deletions src/Commands/CreateSuperAdmin.php
Expand Up @@ -4,7 +4,7 @@

use A17\Twill\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Factory as ValidatorFactory;

class CreateSuperAdmin extends Command
{
Expand All @@ -22,6 +22,22 @@ class CreateSuperAdmin extends Command
*/
protected $description = "Create the superadmin account";

/**
* @var ValidatorFactory
*/
protected $validatorFactory;

/**
* CreateSuperAdmin constructor.
* @param ValidatorFactory $validatorFactory
*/
public function __construct(ValidatorFactory $validatorFactory)
{
parent::__construct();

$this->validatorFactory = $validatorFactory;
}

/**
* Create super admin account.
*
Expand Down Expand Up @@ -92,7 +108,7 @@ private function setPassword()
*/
private function validateEmail($email)
{
return Validator::make(['email' => $email], [
return $this->validatorFactory->make(['email' => $email], [
'email' => 'required|email|max:255|unique:' . config('twill.users_table'),
])->passes();
}
Expand All @@ -105,7 +121,7 @@ private function validateEmail($email)
*/
private function validatePassword($password)
{
return Validator::make(['password' => $password], [
return $this->validatorFactory->make(['password' => $password], [
'password' => 'required|min:6',
])->passes();
}
Expand Down
21 changes: 18 additions & 3 deletions src/Commands/GenerateBlocks.php
Expand Up @@ -2,8 +2,8 @@

namespace A17\Twill\Commands;

use Illuminate\Support\Facades\File;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class GenerateBlocks extends Command
{
Expand All @@ -21,6 +21,21 @@ class GenerateBlocks extends Command
*/
protected $description = "Generate blocks as single file Vue components from blade views";

/**
* @var Filesystem
*/
protected $filesystem;

/**
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
parent::__construct();

$this->filesystem = $filesystem;
}

/**
* Executes the console command.
*
Expand All @@ -29,7 +44,7 @@ class GenerateBlocks extends Command
public function handle()
{
$this->info("Starting to scan block views directory...");
collect(File::files(resource_path('views/admin/blocks')))->each(function ($viewFile) {
collect($this->filesystem->files(resource_path('views/admin/blocks')))->each(function ($viewFile) {
$blockName = $viewFile->getBasename('.blade.php');

$vueBlockTemplate = view('admin.blocks.' . $blockName, ['renderForBlocks' => true])->render();
Expand All @@ -40,7 +55,7 @@ public function handle()

$vueBlockPath = resource_path('assets/js/blocks/') . 'Block' . title_case($blockName) . '.vue';

File::put($vueBlockPath, $vueBlockContent);
$this->filesystem->put($vueBlockPath, $vueBlockContent);

$this->info("Block " . title_case($blockName) . " generated successfully");
});
Expand Down
13 changes: 10 additions & 3 deletions src/Commands/Install.php
Expand Up @@ -3,8 +3,8 @@
namespace A17\Twill\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\DatabaseManager;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\DB;

class Install extends Command
{
Expand All @@ -27,14 +27,21 @@ class Install extends Command
*/
protected $files;

/**
* @var DatabaseManager
*/
protected $db;

/**
* @param Filesystem $files
* @param DatabaseManager $db
*/
public function __construct(Filesystem $files)
public function __construct(Filesystem $files, DatabaseManager $db)
{
parent::__construct();

$this->files = $files;
$this->db = $db;
}

/**
Expand All @@ -47,7 +54,7 @@ public function handle()
{
//check the database connection before installing
try {
DB::connection()->getPdo();
$this->db->connection()->getPdo();
} catch (\Exception $e) {
$this->error('Could not connect to the database, please check your configuration:' . "\n" . $e);
return;
Expand Down
21 changes: 18 additions & 3 deletions src/Commands/RefreshLQIP.php
Expand Up @@ -3,7 +3,7 @@
namespace A17\Twill\Commands;

use A17\Twill\Models\Media;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\DatabaseManager;
use Illuminate\Console\Command;
use ImageService;

Expand All @@ -23,10 +23,25 @@ class RefreshLQIP extends Command
*/
protected $description = 'Refresh Low Quality Image Placeholders.';

/**
* @var DatabaseManager
*/
protected $db;

/**
* @param DatabaseManager $db
*/
public function __construct(DatabaseManager $db)
{
parent::__construct();

$this->db = $db;
}

// TODO: document this and actually think about moving to queuable job after content type updates
public function handle()
{
DB::table('mediables')->orderBy('id')->chunk(100, function ($attached_medias) {
$this->db->table('mediables')->orderBy('id')->chunk(100, function ($attached_medias) {
foreach ($attached_medias as $attached_media) {
$uuid = Media::withTrashed()->find($attached_media->media_id, ['uuid'])->uuid;

Expand All @@ -41,7 +56,7 @@ public function handle()
$data = file_get_contents($url);
$dataUri = 'data:image/gif;base64,' . base64_encode($data);

DB::table('mediables')->where('id', $attached_media->id)->update(['lqip_data' => $dataUri]);
$this->db->table('mediables')->where('id', $attached_media->id)->update(['lqip_data' => $dataUri]);
}
}
});
Expand Down
1 change: 0 additions & 1 deletion src/Exceptions/Handler.php
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Inspector;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

class Handler extends ExceptionHandler
Expand Down
22 changes: 21 additions & 1 deletion src/Http/Controllers/Admin/DashboardController.php
Expand Up @@ -4,12 +4,32 @@

use A17\Twill\Models\Behaviors\HasMedias;
use Analytics;
use Psr\Log\LoggerInterface as Logger;
use Spatie\Activitylog\Models\Activity;
use Spatie\Analytics\Exceptions\InvalidConfiguration;
use Spatie\Analytics\Period;

class DashboardController extends Controller
{
/**
* @var Logger
*/
protected $logger;

/**
* @var Analytics
*/
protected $analytics;

/**
* @param Logger $logger
*/
public function __construct(Logger $logger)
{
parent::__construct();
$this->logger = $logger;
}

/**
* Displays the Twill dashboard.
*
Expand Down Expand Up @@ -147,7 +167,7 @@ private function getFacts()
['dimensions' => 'ga:date']
);
} catch (InvalidConfiguration $exception) {
\Log::error($exception);
$this->logger->error($exception);
return [];
}

Expand Down
20 changes: 17 additions & 3 deletions src/Http/Controllers/Admin/FeaturedController.php
Expand Up @@ -5,11 +5,25 @@
use A17\Twill\Models\Feature;
use A17\Twill\Repositories\Behaviors\HandleMedias;
use A17\Twill\Repositories\Behaviors\HandleTranslations;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Database\DatabaseManager as DB;

class FeaturedController extends Controller
{
/**
* @var DB
*/
protected $db;

/**
* @param DB $db
*/
public function __construct(DB $db)
{
parent::__construct();

$this->db = $db;
}

/**
* @return array|\Illuminate\View\View
*/
Expand Down Expand Up @@ -186,7 +200,7 @@ private function getFeaturedSources($featuredSection, $search = null)
*/
public function save()
{
DB::transaction(function () {
$this->db->transaction(function () {
collect(request('buckets'))->each(function ($bucketables, $bucketKey) {
Feature::where('bucket_key', $bucketKey)->delete();
foreach (($bucketables ?? []) as $position => $bucketable) {
Expand Down
8 changes: 6 additions & 2 deletions src/Http/Controllers/Admin/FileLibraryController.php
Expand Up @@ -7,6 +7,8 @@
use A17\Twill\Services\Uploader\SignS3UploadListener;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Session\Store as SessionStore;

class FileLibraryController extends ModuleController implements SignS3UploadListener
{
Expand Down Expand Up @@ -48,10 +50,12 @@ class FileLibraryController extends ModuleController implements SignS3UploadList
/**
* @param Application $app
* @param Request $request
* @param Router $router
* @param SessionStore $sessionStore
*/
public function __construct(Application $app, Request $request)
public function __construct(Application $app, Request $request, Router $router, SessionStore $sessionStore)
{
parent::__construct($app, $request);
parent::__construct($app, $request, $router, $sessionStore);
$this->removeMiddleware('can:edit');
$this->middleware('can:edit', ['only' => ['signS3Upload', 'tags', 'store', 'singleUpdate', 'bulkUpdate']]);
$this->endpointType = config('twill.file_library.endpoint_type');
Expand Down

0 comments on commit a6106b7

Please sign in to comment.