From b7eb8017517aa5be23a963fa75a5867044c23bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregorio=20Hern=C3=A1ndez=20Caso?= Date: Mon, 12 Mar 2018 11:53:10 +0100 Subject: [PATCH] Refactor gateway class resolution --- src/Common/GatewayFactory.php | 17 +++++++++++++++-- src/Moneta.php | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Common/GatewayFactory.php b/src/Common/GatewayFactory.php index 832836f..adfcd0e 100644 --- a/src/Common/GatewayFactory.php +++ b/src/Common/GatewayFactory.php @@ -6,14 +6,27 @@ class GatewayFactory { + protected $contextNamespace; + + /** + * GatewayFactory constructor. + * @param string|null $contextNamespace + */ + public function __construct($contextNamespace = null) + { + $this->contextNamespace = $contextNamespace ?: substr(get_class($this), 0, -strlen('\\Common\\GatewayFactory')); + } + + /** * Create a new gateway instance * * @param string $name * @param array $parameters + * @param mixed $context * @return GatewayInterface */ - public function create($name, $parameters = null) + public function create($name, $parameters = null, $context = null) { $class = $this->gatewayClass($name); @@ -53,6 +66,6 @@ private function gatewayClass($name) return $name; } - return 'Gregoriohc\\Moneta\\' . ucfirst($name) . '\\Gateway'; + return $this->contextNamespace . '\\' . ucfirst($name) . '\\Gateway'; } } \ No newline at end of file diff --git a/src/Moneta.php b/src/Moneta.php index 3483b4d..b4c018f 100644 --- a/src/Moneta.php +++ b/src/Moneta.php @@ -30,7 +30,7 @@ class Moneta public static function factory() { if (is_null(self::$factory)) { - self::$factory = new GatewayFactory; + self::$factory = new GatewayFactory(__NAMESPACE__); } return self::$factory;