* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ namespace PrestaShop\Module\PrestashopCheckout\Repository; use Db; use DbQuery; use Exception; use PrestaShop\Module\PrestashopCheckout\Customer\ValueObject\CustomerId; use PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException; use PrestaShop\Module\PrestashopCheckout\PayPal\Customer\ValueObject\PayPalCustomerId; class PayPalCustomerRepository { /** * @var Db */ private $db; /** * @param Db $db */ public function __construct(Db $db) { $this->db = $db; } /** * @param CustomerId $customerId * * @return PayPalCustomerId|null * * @throws PsCheckoutException */ public function findPayPalCustomerIdByCustomerId(CustomerId $customerId) { try { $query = new DbQuery(); $query->select('`paypal_customer_id`'); $query->from('pscheckout_customer'); $query->where(sprintf('`id_customer` = %d', (int) $customerId->getValue())); $customerIdPayPal = $this->db->getValue($query); return $customerIdPayPal ? new PayPalCustomerId($customerIdPayPal) : null; } catch (Exception $exception) { throw new PsCheckoutException('Failed to find PayPal Customer ID', 0, $exception); } } /** * @param CustomerId $customerId * @param PayPalCustomerId $paypalCustomerId * * @return void * * @throws PsCheckoutException */ public function save(CustomerId $customerId, PayPalCustomerId $paypalCustomerId) { try { $this->db->insert( 'pscheckout_customer', [ 'id_customer' => (int) $customerId->getValue(), 'paypal_customer_id' => pSQL($paypalCustomerId->getValue()), ] ); } catch (Exception $exception) { throw new PsCheckoutException('Failed to save PayPal Customer ID', 0, $exception); } } }