ByTIC /
payments
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ByTIC\Payments\Utility; |
||
| 4 | |||
| 5 | use ByTIC\MediaLibrary\Models\MediaProperties\MediaProperties; |
||
| 6 | use ByTIC\MediaLibrary\Models\MediaRecords\MediaRecords; |
||
| 7 | use ByTIC\Payments\Models\PurchaseSessions\PurchaseSessionsTrait; |
||
| 8 | use ByTIC\Payments\Models\Subscriptions\Subscriptions; |
||
| 9 | use ByTIC\Payments\Models\Tokens\Tokens; |
||
| 10 | use ByTIC\Payments\Models\Transactions\Transactions; |
||
| 11 | use Nip\Records\Locator\ModelLocator; |
||
| 12 | use Nip\Records\RecordManager; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Class PaymentsModels |
||
| 16 | * @package ByTIC\Payments\Utility |
||
| 17 | */ |
||
| 18 | class PaymentsModels |
||
| 19 | { |
||
| 20 | protected static $purchaseModel = 'purchases'; |
||
| 21 | protected static $purchaseSessionsModel = 'purchase-sessions'; |
||
| 22 | |||
| 23 | protected static $models = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @return RecordManager |
||
| 27 | */ |
||
| 28 | public static function purchases() |
||
| 29 | { |
||
| 30 | return static::getModels('purchases', 'purchases'); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @return RecordManager |
||
| 35 | */ |
||
| 36 | public static function methods() |
||
| 37 | { |
||
| 38 | return static::getModels('methods', 'payment-methods'); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return PurchaseSessionsTrait |
||
| 43 | */ |
||
| 44 | public static function sessions() |
||
| 45 | { |
||
| 46 | return static::getModels('purchasesSessions', 'purchase-sessions'); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return Transactions |
||
| 51 | */ |
||
| 52 | public static function transactions() |
||
| 53 | { |
||
| 54 | return static::getModels('transactions', 'payments-transactions'); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return Tokens |
||
| 59 | */ |
||
| 60 | public static function tokens() |
||
| 61 | { |
||
| 62 | return static::getModels('tokens', 'payments-tokens'); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return Subscriptions |
||
| 67 | */ |
||
| 68 | public static function subscriptions() |
||
| 69 | { |
||
| 70 | return static::getModels('subscriptions', 'payments-subscriptions'); |
||
| 71 | } |
||
| 72 | |||
| 73 | public static function reset() |
||
| 74 | { |
||
| 75 | static::$models = []; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $type |
||
| 80 | * @param string $default |
||
| 81 | * @return mixed|\Nip\Records\AbstractModels\RecordManager |
||
| 82 | */ |
||
| 83 | protected static function getModels($type, $default) |
||
| 84 | { |
||
| 85 | if (!isset(static::$models[$type])) { |
||
| 86 | $modelManager = static::getConfigVar($type, $default); |
||
| 87 | return static::$models[$type] = ModelLocator::get($modelManager); |
||
| 88 | } |
||
| 89 | |||
| 90 | return static::$models[$type]; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $type |
||
| 95 | * @param null|string $default |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | protected static function getConfigVar($type, $default = null) |
||
| 99 | { |
||
| 100 | if (!function_exists('config')) { |
||
| 101 | return $default; |
||
| 102 | } |
||
| 103 | $varName = 'payments.models.' . $type; |
||
| 104 | $config = config(); |
||
| 105 | if ($config->has($varName)) { |
||
| 106 | return $config->get($varName); |
||
| 107 | } |
||
| 108 | return $default; |
||
| 109 | } |
||
| 110 | } |
||
| 111 |