This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
189 lines
5.2 KiB
PHP
189 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\ApiError;
|
|
use App\Enums\ApiStatus;
|
|
use App\Enums\ArcadiaOrderType;
|
|
use App\Enums\Country;
|
|
use App\Enums\QuestionType;
|
|
use App\Exceptions\Api\V2\ApiErrorException;
|
|
use App\Exceptions\Api\V2\CrmException;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*/
|
|
class GenerateCrmClient extends Command
|
|
{
|
|
private const TARGET = 'clients/V2/src';
|
|
private const CLIENTNAMESPACE = 'LDC\\LDCCRM';
|
|
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'client:generate';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Generate the crm v2 client in clients/V2 folder';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->copyEnums();
|
|
$this->copyExceptions();
|
|
$this->copyRoutesList();
|
|
$this->copyApiConfigInfo();
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Copy config files
|
|
*
|
|
* @return void
|
|
*/
|
|
private function copyApiConfigInfo()
|
|
{
|
|
$this->info('Copy Api Config');
|
|
$rootTarget = app_path('../' . self::TARGET);
|
|
if (File::exists("$rootTarget/../apiConfig.readonly.json")) {
|
|
File::delete("$rootTarget/../apiConfig.readonly.json");
|
|
}
|
|
File::put(
|
|
"$rootTarget/../apiConfig.readonly.json",
|
|
\json_encode(config('api'), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Copy routes into a json file
|
|
*
|
|
* @return void
|
|
*/
|
|
private function copyRoutesList()
|
|
{
|
|
$this->info('Copy Routes');
|
|
$routes = [];
|
|
/** @var \Illuminate\Routing\Route */
|
|
foreach (Route::getRoutes() as $route) {
|
|
if (\strpos($route->getPrefix(), 'v2') !== false) {
|
|
$routes[$route->getName()] = [
|
|
'uri' => $route->uri(),
|
|
'methods' => $route->methods()
|
|
];
|
|
}
|
|
}
|
|
$rootTarget = app_path('../' . self::TARGET);
|
|
if (File::exists("$rootTarget/routes.json")) {
|
|
File::delete("$rootTarget/routes.json");
|
|
}
|
|
File::put(
|
|
"$rootTarget/routes.json",
|
|
\json_encode($routes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Copy Exeption classes
|
|
*
|
|
* @return void
|
|
*/
|
|
private function copyExceptions(): void
|
|
{
|
|
$this->info('Copy Exceptions');
|
|
$rootTarget = app_path('../' . self::TARGET);
|
|
$this->copyToFolder([
|
|
CrmException::class,
|
|
ApiErrorException::class
|
|
], 'Exceptions');
|
|
$this->replaceInPhpFile(
|
|
"$rootTarget/Exceptions/ApiErrorException.php",
|
|
'App\Enums\ApiError',
|
|
'LDC\LDCCRM\Enums\ApiError'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Copy enums classes
|
|
*
|
|
* @return void
|
|
*/
|
|
private function copyEnums(): void
|
|
{
|
|
$this->info('Copy Enums');
|
|
$classes = [
|
|
ApiError::class,
|
|
ApiStatus::class,
|
|
ArcadiaOrderType::class,
|
|
Country::class,
|
|
QuestionType::class
|
|
];
|
|
$this->copyToFolder($classes, 'Enums');
|
|
$rootTarget = app_path('../' . self::TARGET);
|
|
foreach ($classes as $class) {
|
|
$classBasename = class_basename($class);
|
|
$dest = "$rootTarget/Enums/$classBasename.php";
|
|
$this->replaceInPhpFile($dest, 'BaseEnumRoutable', 'BaseEnum');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Copy Classes to specified folder
|
|
*
|
|
* @param array $classes
|
|
* @param string $folder
|
|
* @return void
|
|
*/
|
|
private function copyToFolder(array $classes, string $folder): void
|
|
{
|
|
$rootTarget = app_path('../' . self::TARGET);
|
|
foreach ($classes as $class) {
|
|
$classFileName = \str_replace("App\\$folder\\", '', sprintf('%s.php', $class));
|
|
$classFileName = \str_replace('\\', '/', $classFileName);
|
|
$classBasename = class_basename($class);
|
|
$source = \app_path("$folder/$classFileName");
|
|
$dest = "$rootTarget/$folder/$classBasename.php";
|
|
if (File::exists($dest)) {
|
|
File::delete($dest);
|
|
}
|
|
File::copy($source, $dest);
|
|
$this->replaceInPhpFile($dest, 'namespace App', 'namespace ' . self::CLIENTNAMESPACE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Replaces a specific string with another on a file
|
|
*
|
|
* @param string $source
|
|
* @param string $search
|
|
* @param string $replace
|
|
* @return void
|
|
*/
|
|
private function replaceInPhpFile(string $source, string $search, string $replace): void
|
|
{
|
|
$fContent = File::get($source);
|
|
|
|
// Remove trailing \Api\V2 .
|
|
if ($pos = strpos($fContent, '\Api\V2')) {
|
|
$fContent = substr_replace($fContent, '', $pos, strlen('\Api\V2'));
|
|
}
|
|
|
|
// Replace namespace top level.
|
|
$pos = strpos($fContent, $search);
|
|
if ($pos !== false) {
|
|
File::put($source, \str_replace($search, $replace, $fContent));
|
|
}
|
|
}
|
|
}
|