UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

95 lines (74 loc) 2.25 kB
import Apicart from '@apicart/core-sdk'; import PaymentMethod from '../../Entity/Payment/PaymentMethod'; import { DataSourceInterface } from '../../Contract/DataSource/DataSourceInterface'; import { PaymentMethodDaoInterface } from '../../Contract/Dao/Payment/PaymentMethodDaoInterface'; export default class PaymentMethodDao implements PaymentMethodDaoInterface { private _id: number; private _dataSource: DataSourceInterface; private _paymentMethod: PaymentMethod | null = null; private _paymentMethodPromise: Promise<PaymentMethod | null> | null = null; constructor(dataSource: DataSourceInterface, id: number) { this._dataSource = dataSource; this._id = id; } /** @inheritdoc */ public async getEntity(clearCache = false): Promise<PaymentMethod> { if (!this._paymentMethodPromise && (this._paymentMethod === null || clearCache)) { this._paymentMethodPromise = this._dataSource.findPaymentMethod(this.getId()); const paymentMethod = await this._paymentMethodPromise; if (paymentMethod === null) { Apicart.Utils.Console.error('Payment method with id "' + this.getId() + '" not found.'); } else { this._paymentMethod = paymentMethod; } } if (this._paymentMethodPromise) { await this._paymentMethodPromise; this._paymentMethodPromise = null; } return this._paymentMethod; } /** @inheritdoc */ public getId(): number { return this._id; } /** @inheritdoc */ public async getUid(): Promise<string | null> { return (await this.getEntity()).getUid(); } /** @inheritdoc */ public async getName(): Promise<string> { return (await this.getEntity()).getName(); } /** @inheritdoc */ public async getDescription(): Promise<string | null> { return (await this.getEntity()).getDescription(); } /** @inheritdoc */ public async getImage(): Promise<string | null> { return (await this.getEntity()).getImage(); } /** @inheritdoc */ public async getPrice(): Promise<number> { return (await this.getEntity()).getPrice(); } /** @inheritdoc */ public async getSort(): Promise<number | null> { return (await this.getEntity()).getSort(); } /** @inheritdoc */ public async isEnabled(): Promise<boolean> { return (await this.getEntity()).isEnabled(); } }