Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
| GenerateCrmClient | n/a |
0 / 0 |
n/a |
0 / 0 |
16 | n/a |
0 / 0 |
|||
| handle | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| copyApiConfigInfo | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| copyRoutesList | n/a |
0 / 0 |
n/a |
0 / 0 |
4 | |||||
| copyExceptions | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| copyEnums | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| copyToFolder | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
| replaceInPhpFile | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Enums\ApiError; |
| 6 | use App\Enums\ApiStatus; |
| 7 | use App\Enums\ArcadiaOrderType; |
| 8 | use App\Enums\Country; |
| 9 | use App\Enums\QuestionType; |
| 10 | use App\Exceptions\Api\V2\ApiErrorException; |
| 11 | use App\Exceptions\Api\V2\CrmException; |
| 12 | use Illuminate\Console\Command; |
| 13 | use Illuminate\Support\Facades\File; |
| 14 | use Illuminate\Support\Facades\Route; |
| 15 | |
| 16 | /** |
| 17 | * @codeCoverageIgnore |
| 18 | */ |
| 19 | class GenerateCrmClient extends Command |
| 20 | { |
| 21 | private const TARGET = 'clients/V2/src'; |
| 22 | private const CLIENTNAMESPACE = 'LDC\\LDCCRM'; |
| 23 | |
| 24 | /** |
| 25 | * The name and signature of the console command. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $signature = 'client:generate'; |
| 30 | |
| 31 | /** |
| 32 | * The console command description. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $description = 'Generate the crm v2 client in clients/V2 folder'; |
| 37 | |
| 38 | /** |
| 39 | * Execute the console command. |
| 40 | * |
| 41 | * @return integer |
| 42 | */ |
| 43 | public function handle() |
| 44 | { |
| 45 | $this->copyEnums(); |
| 46 | $this->copyExceptions(); |
| 47 | $this->copyRoutesList(); |
| 48 | $this->copyApiConfigInfo(); |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Copy config files |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | private function copyApiConfigInfo() |
| 58 | { |
| 59 | $this->info('Copy Api Config'); |
| 60 | $rootTarget = app_path('../' . self::TARGET); |
| 61 | if (File::exists("$rootTarget/../apiConfig.readonly.json")) { |
| 62 | File::delete("$rootTarget/../apiConfig.readonly.json"); |
| 63 | } |
| 64 | File::put( |
| 65 | "$rootTarget/../apiConfig.readonly.json", |
| 66 | \json_encode(config('api'), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Copy routes into a json file |
| 72 | * |
| 73 | * @return void |
| 74 | */ |
| 75 | private function copyRoutesList() |
| 76 | { |
| 77 | $this->info('Copy Routes'); |
| 78 | $routes = []; |
| 79 | /** @var \Illuminate\Routing\Route */ |
| 80 | foreach (Route::getRoutes() as $route) { |
| 81 | if (\strpos($route->getPrefix(), 'v2') !== false) { |
| 82 | $routes[$route->getName()] = [ |
| 83 | 'uri' => $route->uri(), |
| 84 | 'methods' => $route->methods() |
| 85 | ]; |
| 86 | } |
| 87 | } |
| 88 | $rootTarget = app_path('../' . self::TARGET); |
| 89 | if (File::exists("$rootTarget/routes.json")) { |
| 90 | File::delete("$rootTarget/routes.json"); |
| 91 | } |
| 92 | File::put( |
| 93 | "$rootTarget/routes.json", |
| 94 | \json_encode($routes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Copy Exeption classes |
| 100 | * |
| 101 | * @return void |
| 102 | */ |
| 103 | private function copyExceptions(): void |
| 104 | { |
| 105 | $this->info('Copy Exceptions'); |
| 106 | $rootTarget = app_path('../' . self::TARGET); |
| 107 | $this->copyToFolder([ |
| 108 | CrmException::class, |
| 109 | ApiErrorException::class |
| 110 | ], 'Exceptions'); |
| 111 | $this->replaceInPhpFile( |
| 112 | "$rootTarget/Exceptions/ApiErrorException.php", |
| 113 | 'App\Enums\ApiError', |
| 114 | 'LDC\LDCCRM\Enums\ApiError' |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Copy enums classes |
| 120 | * |
| 121 | * @return void |
| 122 | */ |
| 123 | private function copyEnums(): void |
| 124 | { |
| 125 | $this->info('Copy Enums'); |
| 126 | $classes = [ |
| 127 | ApiError::class, |
| 128 | ApiStatus::class, |
| 129 | ArcadiaOrderType::class, |
| 130 | Country::class, |
| 131 | QuestionType::class |
| 132 | ]; |
| 133 | $this->copyToFolder($classes, 'Enums'); |
| 134 | $rootTarget = app_path('../' . self::TARGET); |
| 135 | foreach ($classes as $class) { |
| 136 | $classBasename = class_basename($class); |
| 137 | $dest = "$rootTarget/Enums/$classBasename.php"; |
| 138 | $this->replaceInPhpFile($dest, 'BaseEnumRoutable', 'BaseEnum'); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Copy Classes to specified folder |
| 144 | * |
| 145 | * @param array $classes |
| 146 | * @param string $folder |
| 147 | * @return void |
| 148 | */ |
| 149 | private function copyToFolder(array $classes, string $folder): void |
| 150 | { |
| 151 | $rootTarget = app_path('../' . self::TARGET); |
| 152 | foreach ($classes as $class) { |
| 153 | $classFileName = \str_replace("App\\$folder\\", '', sprintf('%s.php', $class)); |
| 154 | $classFileName = \str_replace('\\', '/', $classFileName); |
| 155 | $classBasename = class_basename($class); |
| 156 | $source = \app_path("$folder/$classFileName"); |
| 157 | $dest = "$rootTarget/$folder/$classBasename.php"; |
| 158 | if (File::exists($dest)) { |
| 159 | File::delete($dest); |
| 160 | } |
| 161 | File::copy($source, $dest); |
| 162 | $this->replaceInPhpFile($dest, 'namespace App', 'namespace ' . self::CLIENTNAMESPACE); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Replaces a specific string with another on a file |
| 168 | * |
| 169 | * @param string $source |
| 170 | * @param string $search |
| 171 | * @param string $replace |
| 172 | * @return void |
| 173 | */ |
| 174 | private function replaceInPhpFile(string $source, string $search, string $replace): void |
| 175 | { |
| 176 | $fContent = File::get($source); |
| 177 | |
| 178 | // Remove trailing \Api\V2 . |
| 179 | if ($pos = strpos($fContent, '\Api\V2')) { |
| 180 | $fContent = substr_replace($fContent, '', $pos, strlen('\Api\V2')); |
| 181 | } |
| 182 | |
| 183 | // Replace namespace top level. |
| 184 | $pos = strpos($fContent, $search); |
| 185 | if ($pos !== false) { |
| 186 | File::put($source, \str_replace($search, $replace, $fContent)); |
| 187 | } |
| 188 | } |
| 189 | } |