<?php
namespace front\factory;

class ShopPaymentMethod
{
  private static function repo(): \Domain\PaymentMethod\PaymentMethodRepository
  {
    global $mdb;
    return new \Domain\PaymentMethod\PaymentMethodRepository( $mdb );
  }

  // get_apilo_payment_method_id
  static public function get_apilo_payment_method_id( $payment_method_id ) {
    return self::repo()->getApiloPaymentTypeId( (int)$payment_method_id );
  }

  public static function payment_methods_by_transport( $transport_method_id )
  {
    $transport_method_id = (int)$transport_method_id;
    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = 'payment_methods_by_transport' . $transport_method_id;
    $objectData = $cacheHandler->get( $cacheKey );

    if ( $objectData ) {
      return unserialize( $objectData );
    }

    $payments = self::repo()->forTransport( $transport_method_id );
    $cacheHandler->set( $cacheKey, $payments );

    return $payments;
  }

  public static function is_payment_active( $payment_method_id )
  {
    return self::repo()->isActive( (int)$payment_method_id );
  }

  public static function payment_method( $payment_method_id )
  {
    $payment_method_id = (int)$payment_method_id;
    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = 'payment_method' . $payment_method_id;
    $objectData = $cacheHandler->get( $cacheKey );

    if ( !$objectData )
    {
      $payment_method = self::repo()->findActiveById( $payment_method_id );
      $cacheHandler->set( $cacheKey, $payment_method );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $payment_method;
  }

  public static function payment_methods()
  {
    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = 'payment_methods';
    $objectData = $cacheHandler->get( $cacheKey );

    if ( $objectData ) {
      return unserialize( $objectData );
    }

    $payment_methods = self::repo()->allActive();
    $cacheHandler->set( $cacheKey, $payment_methods );

    return $payment_methods;
  }
}
