Skip to content

Commit

Permalink
Merge branch 'dev' Code improvements
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
  • Loading branch information
RTippin committed Aug 10, 2021
2 parents 4930dd3 + 2b28212 commit 594a8f0
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 257 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"require": {
"php": "^7.4||^8.0",
"fakerphp/faker": "^1.13",
"guzzlehttp/guzzle": "^7.3",
"rtippin/messenger": "~0.43"
},
"require-dev": {
Expand Down
52 changes: 18 additions & 34 deletions src/Commands/AudioCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace RTippin\MessengerFaker\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use RTippin\MessengerFaker\MessengerFaker;
use Throwable;

class AudioCommand extends Command
class AudioCommand extends BaseFakerCommand
{
/**
* The name and signature of the console command.
Expand All @@ -32,50 +28,38 @@ class AudioCommand extends Command
/**
* Execute the console command.
*
* @param MessengerFaker $faker
* @return void
* @throws Throwable
*/
public function handle(MessengerFaker $faker): void
public function handle(): void
{
try {
$faker->setThreadWithId($this->argument('thread'), $this->option('admins'))
->setDelay($this->option('delay'));
} catch (ModelNotFoundException $e) {
$this->error('Thread not found.');

if (! $this->initiateThread()) {
return;
}

if (! is_null($this->option('url'))) {
$message = $this->option('url');
} else {
$message = 'a random audio file from '.config('messenger-faker.paths.audio');
}
$this->line('');
$this->info("Found {$faker->getThreadName()}, now messaging audio...");
$this->info("Using {$message}");
$this->info("Found {$this->faker->getThreadName()}, now messaging audio...");
$this->info('Using '.($this->option('url') ?? 'a random audio file from '.config('messenger-faker.paths.audio')));
$this->line('');
$bar = $this->output->createProgressBar($this->option('count'));
$bar->start();

$this->startProgressBar();

try {
for ($x = 1; $x <= $this->option('count'); $x++) {
$faker->audio($this->option('count') <= $x, $this->option('url'));
$bar->advance();
$this->faker->audio(
$this->option('count') <= $x,
$this->option('url')
);

$this->advanceProgressBar();
}
} catch (Exception $e) {
$this->line('');
$this->line('');
$this->error($e->getMessage());
} catch (Throwable $e) {
$this->exceptionMessageOutput($e);

return;
}

$bar->finish();
$this->line('');
$this->line('');
$this->info("Finished sending {$this->option('count')} audio messages to {$faker->getThreadName()}!");
$this->line('');
$this->finishProgressBar();

$this->outputFinalMessage('audio messages', $this->option('count'));
}
}
122 changes: 122 additions & 0 deletions src/Commands/BaseFakerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace RTippin\MessengerFaker\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use RTippin\MessengerFaker\MessengerFaker;
use Symfony\Component\Console\Helper\ProgressBar;
use Throwable;

abstract class BaseFakerCommand extends Command
{
/**
* @var MessengerFaker
*/
protected MessengerFaker $faker;

/**
* @var ProgressBar
*/
private ProgressBar $bar;

/**
* @param MessengerFaker $faker
*/
public function __construct(MessengerFaker $faker)
{
parent::__construct();

$this->faker = $faker;
}

/**
* Set the thread on our faker instance when the command is loaded.
*
* @param int|null $loadMessageCount
* @return bool
*/
protected function initiateThread(?int $loadMessageCount = null): bool
{
try {
$this->faker
->setThreadWithId(
$this->argument('thread') ?: null,
$this->hasOption('admins')
? $this->option('admins')
: false
)
->setDelay(
$this->hasOption('delay')
? $this->option('delay')
: 0
);

if (! is_null($loadMessageCount)) {
$this->faker->setMessages($loadMessageCount);
}
} catch (ModelNotFoundException $e) {
$this->error('Thread not found.');

return false;
} catch (Throwable $e) {
$this->exceptionMessageOutput($e);

return false;
}

return true;
}

/**
* Start the progress bar for this command.
*/
protected function startProgressBar(): void
{
$this->bar = $this->output->createProgressBar($this->option('count'));

$this->bar->start();
}

/**
* Advance the progress bar.
*/
protected function advanceProgressBar(): void
{
$this->bar->advance();
}

/**
* Finish the progress bar.
*/
protected function finishProgressBar(): void
{
$this->bar->finish();
}

/**
* Out put the final message.
*
* @param string $message
* @param int|null $count
*/
protected function outputFinalMessage(string $message, ?int $count = null): void
{
$this->line('');
$this->line('');
$this->info('Finished sending'.(! is_null($count) ? ' '.$count : '')." $message to {$this->faker->getThreadName()}!");
$this->line('');
}

/**
* Output our exception message.
*
* @param Throwable $e
*/
protected function exceptionMessageOutput(Throwable $e): void
{
$this->line('');
$this->line('');
$this->error($e->getMessage());
}
}
52 changes: 18 additions & 34 deletions src/Commands/DocumentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace RTippin\MessengerFaker\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use RTippin\MessengerFaker\MessengerFaker;
use Throwable;

class DocumentCommand extends Command
class DocumentCommand extends BaseFakerCommand
{
/**
* The name and signature of the console command.
Expand All @@ -32,50 +28,38 @@ class DocumentCommand extends Command
/**
* Execute the console command.
*
* @param MessengerFaker $faker
* @return void
* @throws Throwable
*/
public function handle(MessengerFaker $faker): void
public function handle(): void
{
try {
$faker->setThreadWithId($this->argument('thread'), $this->option('admins'))
->setDelay($this->option('delay'));
} catch (ModelNotFoundException $e) {
$this->error('Thread not found.');

if (! $this->initiateThread()) {
return;
}

if (! is_null($this->option('url'))) {
$message = $this->option('url');
} else {
$message = 'a random document from '.config('messenger-faker.paths.documents');
}
$this->line('');
$this->info("Found {$faker->getThreadName()}, now messaging documents...");
$this->info("Using {$message}");
$this->info("Found {$this->faker->getThreadName()}, now messaging documents...");
$this->info('Using '.($this->option('url') ?? 'a random document from '.config('messenger-faker.paths.documents')));
$this->line('');
$bar = $this->output->createProgressBar($this->option('count'));
$bar->start();

$this->startProgressBar();

try {
for ($x = 1; $x <= $this->option('count'); $x++) {
$faker->document($this->option('count') <= $x, $this->option('url'));
$bar->advance();
$this->faker->document(
$this->option('count') <= $x,
$this->option('url')
);

$this->advanceProgressBar();
}
} catch (Exception $e) {
$this->line('');
$this->line('');
$this->error($e->getMessage());
} catch (Throwable $e) {
$this->exceptionMessageOutput($e);

return;
}

$bar->finish();
$this->line('');
$this->line('');
$this->info("Finished sending {$this->option('count')} document messages to {$faker->getThreadName()}!");
$this->line('');
$this->finishProgressBar();

$this->outputFinalMessage('document messages', $this->option('count'));
}
}
47 changes: 17 additions & 30 deletions src/Commands/ImageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace RTippin\MessengerFaker\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use RTippin\MessengerFaker\MessengerFaker;
use Throwable;

class ImageCommand extends Command
class ImageCommand extends BaseFakerCommand
{
/**
* The name and signature of the console command.
Expand All @@ -33,54 +29,45 @@ class ImageCommand extends Command
/**
* Execute the console command.
*
* @param MessengerFaker $faker
* @return void
* @throws Throwable
*/
public function handle(MessengerFaker $faker): void
public function handle(): void
{
try {
$faker->setThreadWithId($this->argument('thread'), $this->option('admins'))
->setDelay($this->option('delay'));
} catch (ModelNotFoundException $e) {
$this->error('Thread not found.');

if (! $this->initiateThread()) {
return;
}

if ($this->option('local')) {
$message = 'a random image from '.config('messenger-faker.paths.images');
} else {
$message = is_null($this->option('url')) ? config('messenger-faker.default_image_url') : $this->option('url');
$message = $this->option('url') ?? config('messenger-faker.default_image_url');
}

$this->line('');
$this->info("Found {$faker->getThreadName()}, now messaging images...");
$this->info("Using {$message}");
$this->info("Found {$this->faker->getThreadName()}, now messaging images...");
$this->info("Using $message");
$this->line('');
$bar = $this->output->createProgressBar($this->option('count'));
$bar->start();

$this->startProgressBar();

try {
for ($x = 1; $x <= $this->option('count'); $x++) {
$faker->image(
$this->faker->image(
$this->option('count') <= $x,
$this->option('local'),
$this->option('url')
);
$bar->advance();

$this->advanceProgressBar();
}
} catch (Exception $e) {
$this->line('');
$this->line('');
$this->error($e->getMessage());
} catch (Throwable $e) {
$this->exceptionMessageOutput($e);

return;
}

$bar->finish();
$this->line('');
$this->line('');
$this->info("Finished sending {$this->option('count')} image messages to {$faker->getThreadName()}!");
$this->line('');
$this->finishProgressBar();

$this->outputFinalMessage('image messages', $this->option('count'));
}
}
Loading

0 comments on commit 594a8f0

Please sign in to comment.