| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Traits; |
| 4 |
|
| 5 |
trait ServiceGetter |
| 6 |
{ |
| 7 |
private $configuration; |
| 8 |
private $api; |
| 9 |
|
| 10 |
/** |
| 11 |
* @param $name |
| 12 |
* |
| 13 |
* @return object|null |
| 14 |
*/ |
| 15 |
public function get_service($name = '') |
| 16 |
{ |
| 17 |
if (!is_string($name) || empty($name)) { |
| 18 |
return null; |
| 19 |
} |
| 20 |
|
| 21 |
try { |
| 22 |
$service_name = '\Payplug\PayplugWoocommerce\Service\\'; |
| 23 |
$service_name .= str_replace('_', '', ucwords($name, '_')); |
| 24 |
|
| 25 |
if (!class_exists($service_name)) { |
| 26 |
return null; |
| 27 |
} |
| 28 |
$service = new $service_name(); |
| 29 |
// $this->{$name} = new $service_name(); |
| 30 |
} catch (\Exception $e) { |
| 31 |
return null; |
| 32 |
} |
| 33 |
|
| 34 |
return $service; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @return object|null |
| 39 |
*/ |
| 40 |
public function get_configuration() |
| 41 |
{ |
| 42 |
$this->configuration = empty($this->configuration) |
| 43 |
? $this->get_service('configuration') |
| 44 |
: $this->configuration; |
| 45 |
|
| 46 |
return $this->configuration; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @return object|null |
| 51 |
*/ |
| 52 |
public function get_api() |
| 53 |
{ |
| 54 |
$this->api = empty($this->api) |
| 55 |
? $this->get_service('api') |
| 56 |
: $this->api; |
| 57 |
|
| 58 |
return $this->api; |
| 59 |
} |
| 60 |
} |
| 61 |
|