Skip to content

Commit

Permalink
config/main:adding components; adding tests; adding command to delete…
Browse files Browse the repository at this point in the history
… tmp files;
  • Loading branch information
2amjsouza committed Sep 1, 2023
1 parent 2dc44ff commit 6cfe684
Show file tree
Hide file tree
Showing 27 changed files with 5,115 additions and 5,299 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ yarn-error.log
/.fleet
/.idea
/.vscode
/xdebug
53 changes: 53 additions & 0 deletions app/Console/Commands/DeleteTmpFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class DeleteTmpFiles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tmp-files:clear';

/**
* The console command description.
*
* @var string
*/
protected $description = 'clear temp files';

/**
* Execute the console command.
*/
public function handle()
{
$qrCodeFiles = $this->getFiles('qrcode');
$tmpFiles = $this->getFiles('public/qrcode');

foreach ($qrCodeFiles as $filePath) {
$this->deleteFiles($filePath);
}
foreach ($tmpFiles as $filePath) {
$this->deleteFiles($filePath);
}

$this->info(count($qrCodeFiles) + count($tmpFiles) . ' files deleted');

return 0;
}

protected function getFiles($path): array
{
return Storage::files($path);
}

protected function deleteFiles(string $filePath): void
{
Storage::delete($filePath);
}
}
1 change: 0 additions & 1 deletion app/Factories/QrCodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static function build(int $format, array $content): QrCodeInterface

return new QrCode($qrCodeFormat);
} catch (Exception $exception) {
throw $exception;
throw new Exception('Format must be enum of ' . FormatEnum::class);
}
}
Expand Down
20 changes: 20 additions & 0 deletions app/Livewire/QrCodeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enum\FormatEnum;
use App\Factories\QrCodeFactory;
use Carbon\Carbon;
use Da\QrCode\Label;
use Da\QrCode\QrCode;
use Da\QrCode\Writer\JpgWriter;
Expand Down Expand Up @@ -84,6 +85,8 @@ public function applyLogo(string $path)

public function build()
{
$this->normalizeOptions();

/** @var QrCode $qrCode */
$qrCode = QrCodeFactory::build($this->format, $this->options);

Expand Down Expand Up @@ -135,6 +138,23 @@ public function download(string $extension)
return Storage::download($pathName, 'qrcode');
}

/**
* set the options the meet current's format data type
* @return void
*/
protected function normalizeOptions(): void
{
if ($this->format === FormatEnum::ICal->value) {
$this->options['startTimestamp'] = Carbon::parse($this->options['startTimestamp'])
->timezone(null)
->getTimestamp();

$this->options['endTimestamp'] = Carbon::parse($this->options['endTimestamp'])
->timezone(null)
->getTimestamp();
}
}

public function render()
{
return view('livewire.qr_code_component');
Expand Down
25 changes: 15 additions & 10 deletions app/Livewire/Traits/ColorsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,35 @@ public function initializeColorsTrait()
'b' => 255,
];

$this->strForeground = "#[{$this->foreground['r']},{$this->foreground['g']},{$this->foreground['b']}]";
$this->strBackground = "#[{$this->background['r']},{$this->background['g']},{$this->background['b']}]";
$this->strForeground = "#000000";
$this->strBackground = "#FFFFFF";
}
}

public function applyColors()
{
$this->validate($this->getColorsRules());
$this->background = $this->extractColors($this->strBackground);
$this->foreground = $this->extractColors($this->strForeground);

$this->validate($this->getColorsRules());

$this->dispatch('apply-colors', $this->foreground, $this->background)
->to(QrCodeComponent::class);
}

public function extractColors(string $color)
{
$color = preg_replace('/[\#\[\]]/', null, $color);
$color = explode(',', $color);
list($red, $green, $blue) = array_map(
function ($c) {
return hexdec(str_pad($c, 2, $c));
},
str_split(ltrim($color, '#'), strlen($color) > 4 ? 2 : 1)
);

return [
'r' => $color[0],
'g' => $color[1],
'b' => $color[2],
'r' => $red,
'g' => $green,
'b' => $blue,
];
}

Expand All @@ -61,8 +66,8 @@ public function extractColors(string $color)
public function getColorsRules(): array
{
return [
'strForeground' => 'string|required|regex:/^\#\[\d+\,\d+\,\d+\]$/',
'strBackground' => 'string|required|regex:/^\#\[\d+\,\d+\,\d+\]$/'
'strForeground' => 'string|required|regex:/^\#[A-f,0-9]{6}$/',
'strBackground' => 'string|required|regex:/^\#[A-f,0-9]{6}$/'
];
}
}
21 changes: 2 additions & 19 deletions app/Livewire/Traits/FormsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ protected function getRulesByForamt(int $format): array
],
FormatEnum::ICal->value => [
'summary' => 'string|required',
'startTimestamp' => 'int|required',
'endTimestamp' => 'int|required',
'startTimestamp' => 'date|required',
'endTimestamp' => 'date|required',
],
FormatEnum::MailMessage->value => [
'email' => 'email|required',
Expand Down Expand Up @@ -88,21 +88,4 @@ protected function getRulesByForamt(int $format): array

return $fields[$format];
}

/**
* Build rules in run time for current form/fields associated with current qrcode format
*
* @return array
*/
protected function buildFormatRules(): array
{
$rules = [];

foreach ($this->formFieldsRules as $field => $fieldsRules) {
$field = "form.{$this->activeFormat}.{$field}";
$rules[$field] = $fieldsRules;
}

return $rules;
}
}
2 changes: 1 addition & 1 deletion app/Livewire/Traits/LabelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait LabelTrait
{
public string $label = '';
public int $size = 16;
public string $align;
public string $align = LabelInterface::ALIGN_CENTER;

public function applyLabel()
{
Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Traits/LogoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function applyLogo()
$this->validate($this->getLogoRules());
/** @var TemporaryUploadedFile $filename */
$filename = uniqid() . '.' . $this->file->getClientOriginalExtension();
#$filename->storePubliclyAs('public/qrcode', $filename);

$this->file->storePubliclyAs('public/qrcode', $filename);

$this->dispatch('apply-logo', '../storage/app/public/qrcode/' . $filename, 'local')
Expand Down
Loading

0 comments on commit 6cfe684

Please sign in to comment.