| 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\ErrorsModel;
use App\Model\BlogModel;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Mosaico\BigCommerce\BaseApi;
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 ContentBlogCreateCommand extends Command
{
protected function configure(): void
{
$this
->setName('content:blog:create')
->setDescription('Create Blog BigCommerce Post')
->setHelp('This command allows you create BigCommerce post');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
new ApiController();
$blogModel = new BlogModel();
$errorsModel = new ErrorsModel();
$errorData = [];
$httpMethod = 'POST';
$posts = (object) $blogModel->getPostsCreate();
$array = [];
if ($posts->num_rows > 0) :
$client = new BaseApi();
foreach ($posts->rows as $value) {
$url_name = (string) substr($value['url'], strrpos($value['url'], '/'));
$url_name = str_replace('/', '', $url_name);
$download_image = $value['image_url'];
$output->writeln('url name' . $url_name);
// Store the original filename
$original_name = basename($download_image); // "img1.jpg"
$output->writeln('original name' . $original_name);
// Original extension by string manipulation
$original_extension = (string) substr($original_name, strrpos($original_name, '.')); // ".jpg"
$output->writeln('original extension' . $original_extension);
$types = array(
'image/jpeg' => '.jpg',
'image/png' => '.png',
'image/gif' => '.gif'
// Other types as needed...
);
$img = file_get_contents($download_image);
//print_r($img);
$stored_name = 'public/images/' . $url_name . $original_extension;
$output->writeln('stored name' . $stored_name);
$original_name = str_replace('%20', ' ', $original_name);
$original_name = str_replace('%28', '(', $original_name);
$original_name = str_replace('%29', ')', $original_name);
$image_path = '/var/www/cookingdepot/public/images/';
$image_name = $url_name . $original_extension;
$image_url = $image_path . $image_name;
$output->writeln('image: ' . $image_url);
if ($img) {
shell_exec('wget ' . $download_image);
shell_exec('mv "' . $original_name . '" ' . $image_url);
} else {
$output->writeln('no file');
}
$published_date = gmdate(DATE_RSS, strtotime($value['publish_date']));
$array = [
'title' => $value['title'],
'url' => '/blog/' . $url_name,
'body' => $value['body'],
'tags' => [
$value['tags']
],
'is_published' => true,
'meta_description' => $value['meta_description'],
'meta_keywords' => '',
'author' => $value['author'],
'thumbnail_path' => $image_name,
'published_date' => $published_date
];
try {
$handler = HandlerStack::create();
$tapMiddleware = Middleware::tap(function ($request) {
echo $request->getBody() . "\r\n";
});
$response = $client->api()->request($httpMethod, 'blog/posts', [
'headers' => [
'X-Auth-Token' => $client->token(),
'Accept' => 'application/json',
],
'content-type' => 'application/json',
'json' => $array,
'handler' => $tapMiddleware($handler),
]);
$body = $response->getBody();
$post = json_decode((string) $body, true);
print_r($post);
//$post = $response->getBody();
//return 0;
if ($post) {
$blogModel->updateIdBigcommerce(
(int) $value['id_blog'],
(int) $post['id']
);
$this->logInfo([
'status' => $response->getStatusCode(),
'title' => $value['title'],
'id' => $post['id'],
]);
}
$output->writeln($value['id_blog'] . '-' . $value['title']);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$error = json_decode((string) $response->getBody());
$error_string = '[' . date('y-m-d h:i:s') . '] createBlog.ERROR: ' . (string) $response->getBody();
//$this->logError($error, $value);
print_r($error);
$blogModel->updateStatusErrorBigcommerce(
(int) $value['id_blog'],
(int) 2,
(string) addslashes($error_string)
);
$output->writeln($error_string);
$arrayErrors = (array) $error->errors;
$errorData['id_api'] = 3;
$errorData['id_resource'] = $value['id_blog'];
$errorData['request_method'] = $httpMethod;
$errorData['status_code'] = $error->status;
$errorData['title'] = addslashes($error->title);
$errorData['detail'] = addslashes(array_key_first($arrayErrors) .
' - ' . reset($arrayErrors));
$errorsModel->addResponseError($errorData);
}
}
else :
$output->writeln('No new data');
endif;
return 0;
}
public function logError(object $error, array $post): void
{
$log = new Logger('CreateBlogPost');
$log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/blog/error.log', Logger::ERROR));
$errors = (array) $error->errors;
$log->error(
';status: ' . $error->status . ';',
[
'title' => $error->title,
'id_blog' => $post['id_blog'],
'title' => $post['title'],
'errors' => $errors,
]
);
}
public function logInfo(array $info)
{
$log = new Logger('CreatePost3');
$log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/blog/info.log', Logger::INFO));
$log->info(
'status: ' . $info['status'],
[
'Post Added: ' . $info['title'],
'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');
}
}