Aramızdan Ayrılanlar - Vefat Edenler
<?php
namespace AppServices;
use SoapClient;
use SoapFault;
use Exception;
use PsrLogLoggerInterface;
class ShipmentService
{
/**
* @var SoapClient
*/
protected $client;
/**
* @var array
*/
protected $config;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* ShipmentService constructor.
*
* @param array $config
* @param LoggerInterface $logger
*/
public function __construct(array $config, LoggerInterface $logger)
{
$this->config = $config;
$this->logger = $logger;
try {
$this->initializeClient();
} catch (Exception $e) {
$this->logger->error('SOAP client initialization failed', [
'error' => $e->getMessage(),
'wsdl' => $config['surat']['wsdl'] ?? 'undefined'
]);
throw $e;
}
}
/**
* Initialize the SOAP client
*/
protected function initializeClient()
{
$options = [
'trace' => 1,
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => true,
'connection_timeout' => 30
];
if (!isset($this->config['surat']['wsdl'])) {
throw new Exception('WSDL URL is not defined in configuration');
}
$this->client = new SoapClient($this->config['surat']['wsdl'], $options);
}
/**
* Get shipments between given dates
*
* @param string $startDate Start date in Y-m-d format
* @param string $endDate End date in Y-m-d format
* @param bool $useWebOrderCode Whether to use web order code
* @return array
* @throws Exception
*/
public function getShipments($startDate, $endDate, $useWebOrderCode = false)
{
if (!$this->validateDateFormat($startDate) || !$this->validateDateFormat($endDate)) {
throw new Exception('Invalid date format. Use Y-m-d format.');
}
$params = [
'GonderenCariKodu' => $this->config['surat']['cari_kodu'] ?? '',
'WebPassword' => $this->config['surat']['password'] ?? '',
'BasTar' => $startDate,
'BitTar' => $endDate,
'IsWebSiparisKoduOlsun' => $useWebOrderCode
];
try {
$result = $this->request('CariKoduveSifre', $params);
$this->logger->info('Shipments retrieved successfully', [
'start_date' => $startDate,
'end_date' => $endDate
]);
return $result;
} catch (Exception $e) {
$this->logger->error('Failed to retrieve shipments', [
'error' => $e->getMessage(),
'start_date' => $startDate,
'end_date' => $endDate
]);
throw $e;
}
}
/**
* Make a SOAP request
*
* @param string $method
* @param array $params
* @return mixed
* @throws SoapFault
*/
protected function request($method, $params)
{
try {
$response = $this->client->$method($params);
return $this->processResponse($response);
} catch (SoapFault $fault) {
$this->logger->error('SOAP request failed', [
'method' => $method,
'fault_code' => $fault->faultcode,
'fault_string' => $fault->faultstring
]);
throw $fault;
}
}
/**
* Process and normalize the SOAP response
*
* @param mixed $response
* @return array
*/
protected function processResponse($response)
{
// Burada response'u işleyebilir, dönüştürebilir veya doğrulayabilirsiniz
// Örneğin XML'i array'e çevirebilir veya belirli alanları kontrol edebilirsiniz
return $response;
}
/**
* Validate date format
*
* @param string $date
* @return bool
*/
protected function validateDateFormat($date)
{
$d = DateTime::createFromFormat('Y-m-d', $date);
return $d && $d->format('Y-m-d') === $date;
}
/**
* Get last request XML
*
* @return string
*/
public function getLastRequestXml()
{
return $this->client->__getLastRequest();
}
/**
* Get last response XML
*
* @return string
*/
public function getLastResponseXml()
{
return $this->client->__getLastResponse();
}
}
GitHubda buldum