| 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\ClientesModel;
use App\Model\ErrorsModel;
use GuzzleHttp\Client;
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 CustomerAddressCreateCommand extends Command
{
protected function configure(): void
{
$this
->setName('customers:address:create')
->setDescription('Create BigCommerce Customer Address')
->setHelp('This command allows you to create BigCommerce customer address');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
new ApiController();
$client = new BaseApiHandlerV3();
$clientesModel = new ClientesModel();
$customers = (object) $clientesModel->getCustomerAddressCreate();
$arrayAddress = [];
$errorsModel = new ErrorsModel();
$errorData = [];
$httpMethod = 'POST';
$addresses = (object) $clientesModel->getAddressCreate((int) $customer['numero_cliente']);
//echo $estado;
//print_r($addresses);
//$estado = utf8_decode($addresses->row['estado']);
if ($addresses->num_rows > 0) :
foreach ($addresses->rows as $value) :
//$estado = $value['estado'];
$estado = (string) utf8_decode($value['estado']);
$output->writeln($estado);
$address = [
[
'customer_id' => (int) $value['customer_id'],
'first_name' => $value['first_name'],
'last_name' => $value['last_name'],
'address1' => $value['calle'],
'address2' => $value['colonia'],
'city' => $value['ciudad'],
'state_or_province' => $estado,
'country_code' => $value['code_iso_2'],
'company' => '',
'phone' => $value['telefono_1'],
'postal_code' => $value['codigo_postal'] ?? '00000',
'address_type' => 'commercial'
]
];
try {
$handler = HandlerStack::create();
$tapMiddleware = Middleware::tap(function ($request) {
//echo $request->getHeaderLine('Content-type') . "\r\n";
echo $request->getBody() . "\r\n";
});
$response = $client->api()->request('POST', 'customers/addresses', [
'headers' => [
'X-Auth-Token' => $client->token(),
],
'content-type' => 'application/json',
'json' => $address,
'handler' => $tapMiddleware($handler),
]);
$body = $response->getBody();
$data = json_decode((string) $body, true);
if (isset($data['data'][0])) :
$clientesModel->updateCustomerAddressIsCreate(
(int) $customer['id_cliente']
);
foreach ($address as $key => $value) :
$clientesModel->updateAddressIdBigCommerce(
(int) $value['id_direccion'],
(int) $data['data'][$key]['id']
);
$this->logInfo([
'status' => $response->getStatusCode(),
'id_cliente' => $customer['id_cliente'],
'id' => $data['data'][$key]['id'],
'id_customer' => $customer['id_bigcommerce'],
]);
endforeach;
endif;
} catch (\GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$error = json_decode((string) $response->getBody());
//$this->logError($error, $customer);
print_r($error);
$clientesModel->updateAddressStatusBigcommerce(
(int) $value['id_direccion'],
2
);
$arrayErrors = (array) $error->errors;
$errorData['id_api'] = $errorsModel->getApiId('create_address');
$errorData['id_resource'] = $value['id_direccion'];
$errorData['request_method'] = $httpMethod;
$errorData['status_code'] = $error->status;
$errorData['title'] = $error->title;
$errorData['detail'] = array_key_first($arrayErrors) .
'-' . reset($arrayErrors);
$errorsModel->addResponseError($errorData);
}
endforeach;
else :
$output->writeln('No Data');
endif;
return 0;
}
public function logError(object $error, array $customer): void
{
$log = new Logger('createCustomerAddress');
$log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/customers/address/error.log', Logger::ERROR));
$errors = (array) $error;
$log->error(
'status: ' . $error->status,
[
'title' => $error->title,
"cliente" => $customer['id_cliente'],
'customer' => $customer['id_bigcommerce'],
$errors,
]
);
}
public function logInfo(array $info)
{
$log = new Logger('CreateCustomerAddress');
$log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/customers/address/info.log', Logger::INFO));
$log->info(
'status: ' . $info['status'],
[
'Create Customer Address: ' .
$info['id_customer'] . ' / ' .
$info['id'],
'Cliente ID: ' . $info['id_cliente'],
]
);
}
public function debugError(object $error)
{
$errors = (array) $error;
print_r($errors);
}
}