| Server IP : 74.208.250.37 / Your IP : 216.73.216.114 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ubuntu 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64 User : miferval ( 1000) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/brainwavemx/app/Console/ |
Upload File : |
<?php
declare(strict_types=1);
namespace App\Console;
use App\Controller\Api\ApiController;
use App\Model\ProductosModel;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Mosaico\BigCommerce\BaseApiHandlerV3;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CatalogBrandCreateCommand extends Command
{
protected function configure(): void
{
$this
->setName('catalog:brand:create')
->setDescription('Create BigCommerce Brand')
->setHelp('This command allows you create BigCommerce brands');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
new ApiController();
$productosModel = new ProductosModel();
$brands = (object) $productosModel->getBrandsCreate();
$array = [];
if ($brands->num_rows > 0) :
$client = new BaseApiHandlerV3();
;
foreach ($brands->rows as $value) {
$array = [
'name' => $value['nombre_marca'],
'page_title' => $value['nombre_marca'],
];
try {
$handler = HandlerStack::create();
$tapMiddleware = Middleware::tap(function ($request) {
echo $request->getBody() . "\r\n";
});
$response = $client->api()->request('POST', 'catalog/brands', [
'headers' => [
'X-Auth-Token' => $client->token(),
],
'content-type' => 'application/json',
'json' => $array,
'handler' => $tapMiddleware($handler),
]);
$body = $response->getBody();
$brand = json_decode((string) $body, true);
//print_r($product);
//$product = $response->getBody();
//return 0;
if ($brand) {
$productosModel->updateMarcaIdBigcommerce(
(int) $value['id_marca'],
(int) $brand['data']['id']
);
$this->logInfo([
'status' => $response->getStatusCode(),
'marca' => $value['nombre_marca'],
'id' => $brand['data']['id'],
]);
}
//$output->writeln($value['id_marca'] . '-' . $value['nombre_marca']);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$error = json_decode((string) $response->getBody());
$this->logError($error, $value);
}
}
else :
$output->writeln('No new data');
endif;
return 0;
}
public function logError(object $error, array $marca): void
{
$log = new Logger('CreateBrandV3');
$log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/catalog/error-marca.log', Logger::ERROR));
$errors = (array) $error->errors;
$log->error(
'status: ' . $error->status,
[
'title' => $error->title,
'id' => $marca['id_marca'],
$errors,
]
);
}
public function logInfo(array $info)
{
$log = new Logger('CreateBrandV3');
$log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/catalog/info-marca.log', Logger::INFO));
$log->info(
'status: ' . $info['status'],
[
'Brand Added: ' . $info['marca'],
'BigCommerce ID: ' . $info['id'],
]
);
}
public function debugError(object $error, OutputInterface $output)
{
$output->writeln('error');
$output->writeln($error->message);
$output->writeln($error->details->conflict_reason);
$output->writeln('errorEof');
}
}