| 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\CustomersModel;
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 CustomerCreateCommand extends Command
{
protected function configure(): void
{
$this
->setName('customers:create')
->setDescription('Create BigCommerce Customers')
->setHelp('This command allows you to create BigCommerce customers');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
new ApiController();
$model = new CustomersModel();
$customers = (object) $model->getCustomerCreate();
$array = [];
$errorsModel = new ErrorsModel();
$errorData = [];
$httpMethod = 'POST';
if ($customers->num_rows > 0) :
$client = new BaseApiHandlerV3();
foreach ($customers->rows as $value) {
print_r($value);
$array = [
[
'email' => $value['email'],
'first_name' => $value['firstname'],
'last_name' => $value['lastname'],
'customer_group_id' => (int) $value['id_group_bc'],
],
];
try {
$handler = HandlerStack::create();
$tapMiddleware = Middleware::tap(function ($request) {
echo $request->getBody() . "\r";
});
$response = $client->api()->request($httpMethod, 'customers', [
'headers' => [
'X-Auth-Token' => $client->token(),
],
'content-type' => 'application/json',
'json' => $array,
'handler' => $tapMiddleware($handler),
]);
$body = $response->getBody();
$customer = json_decode((string) $body, true);
$model->updateIdBigcommerce(
(int) $value['id_customer'],
(int) $customer['data'][0]['id']
);
$this->logInfo([
'status' => $response->getStatusCode(),
'id' => $customer['data'][0]['id'],
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$error = json_decode((string) $response->getBody());
$error_string = '[' . date('Y-m-d h:i:s') . '] createCustomer.ERROR: cliente: ' . $value['id_customer'] . ' error: ' . (string) $response->getBody();
$output->writeln($error_string);
$model->updateStatusErrorBigcommerce(
(int) $value['id_customer'],
(int) 2,
(string) $error_string
);
$this->logError($error, $value);
$arrayErrors = (array) $error->errors;
$errorData['id_api'] = $errorsModel->getApiId('create_customer');
$errorData['id_resource'] = $value['id_customer'];
$errorData['request_method'] = $httpMethod;
$errorData['status_code'] = $error->status;
$errorData['title'] = $error->title;
$errorData['detail'] = array_key_first($arrayErrors) .
'-' . reset($arrayErrors);
$errorsModel->addResponseError($errorData);
}
}
else :
$output->writeln('No new data');
endif;
return 0;
}
public function logError(object $error, array $cliente): void
{
$log = new Logger('createCustomer');
$log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/customers/error.log', Logger::ERROR));
$arrayErrors = (array) $error->errors;
$log->error(
'status: ' . $error->status,
[
'title' => $error->title,
'type' => $error->type,
'detail' => array_key_first($arrayErrors) . '-' . reset($arrayErrors),
'cliente' => $cliente['id_customer'],
]
);
}
public function logInfo(array $info)
{
$log = new Logger('CreateCustomer');
$log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/customers/info.log', Logger::INFO));
$log->info(
'status: ' . $info['status'],
[
'Create Customers: ' . $info['id'],
]
);
}
public function debugError(object $error)
{
$errors = (array) $error;
print_r($errors);
}
}