Skip to content

Commit

Permalink
- implement data update operations for sync-status route
Browse files Browse the repository at this point in the history
  • Loading branch information
bennanis committed Nov 11, 2024
1 parent 5947d27 commit b6f9fd9
Show file tree
Hide file tree
Showing 17 changed files with 745 additions and 351 deletions.
72 changes: 37 additions & 35 deletions Data/CustomersAddressesData.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
<?php

namespace Shopimind\Data;

use Thelia\Model\Address;
use Thelia\Model\CountryQuery;

class CustomersAddressesData
{
/**
* Formats the customer address data to match the Shopimind format.
*
* @param Address $address
*/
public static function formatCustomerAddress( Address $address ){
$country = CountryQuery::create()->findOneById( $address->getCountryId() );

$data = [
"address_id" => intval( $address->getId() ),
"first_name" => $address->getFirstname() ?? '',
"last_name" => $address->getLastname() ?? '',
"primary_phone" => $address->getPhone() ?? null,
"secondary_phone" => $address->getCellphone() ?? null,
"company" => $address->getCompany() ?? null,
"address_line_1" => $address->getAddress1() ?? '',
"address_line_2" => $address->getAddress2() ?? '',
"postal_code" => $address->getZipcode() ?? '',
"city" => $address->getCity() ?? '',
"country" => !empty( $country ) ? $country->getTitle() : '',
"is_active" => true
];

return $data;
}
}
<?php

namespace Shopimind\Data;

use Thelia\Model\Address;
use Thelia\Model\CountryQuery;

class CustomersAddressesData
{
/**
* Formats the customer address data to match the Shopimind format.
*
* @param Address $address
*/
public static function formatCustomerAddress( Address $address ){
$country = CountryQuery::create()->findOneById( $address->getCountryId() );

$data = [
"address_id" => intval( $address->getId() ),
"first_name" => $address->getFirstname() ?? '',
"last_name" => $address->getLastname() ?? '',
"primary_phone" => $address->getPhone() ?? null,
"secondary_phone" => $address->getCellphone() ?? null,
"company" => $address->getCompany() ?? null,
"address_line_1" => $address->getAddress1() ?? '',
"address_line_2" => $address->getAddress2() ?? '',
"postal_code" => $address->getZipcode() ?? '',
"city" => $address->getCity() ?? '',
"country" => !empty( $country ) ? $country->getTitle() : '',
"is_active" => true,
"created_at" => $address->getCreatedAt()->format('Y-m-d\TH:i:s.u\Z'),
'updated_at' => $address->getUpdatedAt()->format('Y-m-d\TH:i:s.u\Z'),
];

return $data;
}
}
142 changes: 72 additions & 70 deletions Data/ProductImagesData.php
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
<?php

namespace Shopimind\Data;

use Thelia\Model\ProductImage;
use Thelia\Core\Event\Image\ImageEvent;
use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Model\Base\Lang;

class ProductImagesData
{
/**
* Formats the product image data to match the Shopimind format.
*
* @param ProductImage $productImage
* @param $imageTranslated
* @param $imageDefault
* @param EventDispatcherInterface $dispatcher
* @param string $action
* @return array
*/
public static function formatProductImage( ProductImage $productImage, Lang $lang, EventDispatcherInterface $dispatcher, $action = 'insert' ): array
{
$data = [];
if ( $action == 'insert' ) {
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://";
$serverName = (array_key_exists('SERVER_NAME',
$_SERVER) ? $_SERVER['SERVER_NAME'] : array_key_exists('HTTP_HOST',
$_SERVER)) ? $_SERVER['HTTP_HOST'] : 'localhost';
$scriptPath = dirname($_SERVER['SCRIPT_NAME']);
$rootUrl = $protocol . $serverName . rtrim($scriptPath, '/') . '/';

$data = [
'image_id' => strval( $productImage->getId() ),
'variation_id' => null,
'lang' => $lang->getCode(),
'url' => $rootUrl,
'is_default' => false,
];
} else if ( $action == 'update') {
$url = '';

try {
$imgSourcePath = $productImage->getUploadDir().DS.$productImage->getFile();

$productImageEvent = new ImageEvent();
$productImageEvent->setSourceFilepath($imgSourcePath)->setCacheSubdirectory('product_image');

$dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS);
$url = $productImageEvent->getFileUrl();
} catch (\Throwable $th) {
$url = null;
}


$productSaleElementsProductImages = $productImage->getProductSaleElementsProductImages()->getFirst();

$data = [
'image_id' => strval( $productImage->getId() ),
'variation_id' => $productSaleElementsProductImages ? intval( $productSaleElementsProductImages->getProductSaleElementsId() ) : null,
'lang' => $lang->getCode(),
'url' => $url,
'is_default' => ( $productImage->getPosition() == 1 ) ? true : false,
];
}

return $data;
}
}
<?php

namespace Shopimind\Data;

use Thelia\Model\ProductImage;
use Thelia\Core\Event\Image\ImageEvent;
use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Model\Base\Lang;

class ProductImagesData
{
/**
* Formats the product image data to match the Shopimind format.
*
* @param ProductImage $productImage
* @param $imageTranslated
* @param $imageDefault
* @param EventDispatcherInterface $dispatcher
* @param string $action
* @return array
*/
public static function formatProductImage( ProductImage $productImage, Lang $lang, EventDispatcherInterface $dispatcher, $action = 'insert' ): array
{
$data = [];
if ( $action == 'insert' ) {
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://";
$serverName = (array_key_exists('SERVER_NAME',
$_SERVER) ? $_SERVER['SERVER_NAME'] : array_key_exists('HTTP_HOST',
$_SERVER)) ? $_SERVER['HTTP_HOST'] : 'localhost';
$scriptPath = dirname($_SERVER['SCRIPT_NAME']);
$rootUrl = $protocol . $serverName . rtrim($scriptPath, '/') . '/';

$data = [
'image_id' => strval( $productImage->getId() ),
'variation_id' => null,
'lang' => $lang->getCode(),
'url' => $rootUrl,
'is_default' => false,
];
} else if ( $action == 'update') {
$url = '';

try {
$imgSourcePath = $productImage->getUploadDir().DS.$productImage->getFile();

$productImageEvent = new ImageEvent();
$productImageEvent->setSourceFilepath($imgSourcePath)->setCacheSubdirectory('product_image');

$dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS);
$url = $productImageEvent->getFileUrl();
} catch (\Throwable $th) {
$url = null;
}


$productSaleElementsProductImages = $productImage->getProductSaleElementsProductImages()->getFirst();

$data = [
'image_id' => strval( $productImage->getId() ),
'variation_id' => $productSaleElementsProductImages ? intval( $productSaleElementsProductImages->getProductSaleElementsId() ) : null,
'lang' => $lang->getCode(),
'url' => $url,
'is_default' => ( $productImage->getPosition() == 1 ) ? true : false,
'created_at' => $productImage->getCreatedAt()->format('Y-m-d\TH:i:s.u\Z'),
'updated_at' => $productImage->getUpdatedAt()->format('Y-m-d\TH:i:s.u\Z')
];
}

return $data;
}
}
Loading

0 comments on commit b6f9fd9

Please sign in to comment.