Add CryptographyContract and its implementation

Added a new CryptographyContract interface and CryptographyString class that implements this contract. The CryptographyContract encapsulates the encryption and decryption of strings, enforcing these operations to be standardized across the application. The CryptographyString class uses Laravel's native crypt facades to perform these actions. In the AppServiceProvider, CryptographyContract is now bound to CryptographyString class, allowing the container to automatically resolve the dependencies wherever the interface is type hinted.
This commit is contained in:
Leonid Nikitin 2023-09-19 14:29:01 +06:00
parent 27046e6674
commit c3e4c68a41
Signed by: kor-elf
GPG Key ID: 7DE8F80C5CEC2C0D
3 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,9 @@
<?php declare(strict_types=1);
namespace App\Contracts;
interface CryptographyContract
{
public function encrypt(string $text): string;
public function decrypt(string $encryptedText): string;
}

View File

@ -10,8 +10,10 @@ use App\Captcha\Images\Body;
use App\Captcha\Images\Head; use App\Captcha\Images\Head;
use App\Captcha\Images\ImageManager; use App\Captcha\Images\ImageManager;
use App\Captcha\Images\Lines; use App\Captcha\Images\Lines;
use App\Contracts\CryptographyContract;
use App\Services\Api\V1\CaptchaGenerateService; use App\Services\Api\V1\CaptchaGenerateService;
use App\Services\CaptchaToken\CaptchaTokenHandler; use App\Services\CaptchaToken\CaptchaTokenHandler;
use App\Services\CryptographyString;
use App\Services\GenerateTokenCommand\GenerateTokenUlidCommand; use App\Services\GenerateTokenCommand\GenerateTokenUlidCommand;
use App\Services\GenerateTokenCommand\GenerateTokenUuidCommand; use App\Services\GenerateTokenCommand\GenerateTokenUuidCommand;
use App\Services\Search\CreateSearchInstanceCommand; use App\Services\Search\CreateSearchInstanceCommand;
@ -35,6 +37,8 @@ final class AppServiceProvider extends ServiceProvider
$this->app->bind(ImageBody::class, Body::class); $this->app->bind(ImageBody::class, Body::class);
$this->app->bind(ImageLines::class, Lines::class); $this->app->bind(ImageLines::class, Lines::class);
$this->app->bind(CryptographyContract::class, CryptographyString::class);
$this->app->bind(CreateSearchInstanceCommand::class, function () { $this->app->bind(CreateSearchInstanceCommand::class, function () {
return new CreateSearchInstanceCommand(Search::class); return new CreateSearchInstanceCommand(Search::class);
}); });

View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
namespace App\Services;
use App\Contracts\CryptographyContract;
use Illuminate\Support\Facades\Crypt;
final class CryptographyString implements CryptographyContract
{
public function encrypt(string $text): string
{
return Crypt::encryptString($text);
}
public function decrypt(string $encryptedText): string
{
return Crypt::decryptString($encryptedText);
}
}