diff --git a/PayumBuilder.php b/PayumBuilder.php index 4ee5872..89736cb 100644 --- a/PayumBuilder.php +++ b/PayumBuilder.php @@ -9,6 +9,7 @@ use Payum\Core\Bridge\PlainPhp\Security\TokenFactory; use Payum\Core\Exception\InvalidArgumentException; use Payum\Core\Extension\GenericTokenFactoryExtension; +use Payum\Core\Extension\StorageExtension; use Payum\Core\Model\ArrayObject; use Payum\Core\Model\Payment; use Payum\Core\Model\Token; @@ -16,6 +17,7 @@ use Payum\Core\Registry\FallbackRegistry; use Payum\Core\Registry\RegistryInterface; use Payum\Core\Registry\SimpleRegistry; +use Payum\Core\Registry\StorageRegistryInterface; use Payum\Core\Security\GenericTokenFactory; use Payum\Core\Security\GenericTokenFactoryInterface; use Payum\Core\Security\HttpRequestVerifierInterface; @@ -37,25 +39,15 @@ class PayumBuilder { /** - * @var HttpRequestVerifierInterface + * @var HttpRequestVerifierInterface|callable|null */ protected $httpRequestVerifier; /** - * @var callable - */ - protected $httpRequestVerifierBuilder; - - /** - * @var TokenFactoryInterface + * @var TokenFactoryInterface|callable|null */ protected $tokenFactory; - /** - * @var callable - */ - protected $tokenFactoryBuilder; - /** * @var GenericTokenFactoryInterface */ @@ -72,15 +64,10 @@ class PayumBuilder protected $tokenStorage; /** - * @var GatewayFactoryInterface + * @var GatewayFactoryInterface|callable|null */ protected $coreGatewayFactory; - /** - * @var callable - */ - protected $coreGatewayFactoryBuilder; - /** * @var array */ @@ -189,6 +176,150 @@ public function addGatewayFactory($name, GatewayFactoryInterface $gatewayFactory return $this; } + /** + * @param HttpRequestVerifierInterface|callable|null $httpRequestVerifier + * + * @return static + */ + public function setHttpRequestVerifier($httpRequestVerifier = null) + { + if ( + null === $httpRequestVerifier || + $httpRequestVerifier instanceof HttpRequestVerifierInterface || + is_callable($httpRequestVerifier)) + { + $this->httpRequestVerifier = $httpRequestVerifier; + + return $this; + } + + throw new InvalidArgumentException('Invalid argument'); + } + + /** + * @param TokenFactoryInterface|callable|null $tokenFactory + * + * @return static + */ + public function setTokenFactory($tokenFactory = null) + { + if ( + null === $tokenFactory || + $tokenFactory instanceof TokenFactoryInterface || + is_callable($tokenFactory)) + { + $this->tokenFactory = $tokenFactory; + + return $this; + } + + throw new InvalidArgumentException('Invalid argument'); + } + + /** + * @param GenericTokenFactoryInterface $tokenFactory + * + * @return static + */ + public function setGenericTokenFactory(GenericTokenFactoryInterface $tokenFactory = null) + { + $this->genericTokenFactory = $tokenFactory; + + return $this; + } + + /** + * @param \string[] $paths + * + * @return static + */ + public function setGenericTokenFactoryPaths(array $paths = []) + { + $this->genericTokenFactoryPaths = $paths; + + return $this; + } + + /** + * @param StorageInterface $tokenStorage + * + * @return static + */ + public function setTokenStorage(StorageInterface $tokenStorage = null) + { + $this->tokenStorage = $tokenStorage; + + return $this; + } + + /** + * @param GatewayFactoryInterface|callable|null $coreGatewayFactory + * + * @return static + */ + public function setCoreGatewayFactory($coreGatewayFactory = null) + { + if ( + null === $coreGatewayFactory || + $coreGatewayFactory instanceof GatewayFactoryInterface || + is_callable($coreGatewayFactory)) + { + $this->coreGatewayFactory = $coreGatewayFactory; + + return $this; + } + + throw new InvalidArgumentException('Invalid argument'); + } + + /** + * @param array $config + * + * @return static + */ + public function setCoreGatewayFactoryConfig(array $config = null) + { + $this->coreGatewayFactoryConfig = $config; + + return $this; + } + + /** + * @param StorageInterface $gatewayConfigStorage + * + * @return static + */ + public function setGatewayConfigStorage(StorageInterface $gatewayConfigStorage = null) + { + $this->gatewayConfigStorage = $gatewayConfigStorage; + + return $this; + } + + /** + * @param RegistryInterface $mainRegistry + * + * @return static + */ + public function setMainRegistry(RegistryInterface $mainRegistry = null) + { + $this->mainRegistry = $mainRegistry; + + return $this; + } + + /** + * @param HttpClientInterface $httpClient + * + * @return static + */ + public function setHttpClient(HttpClientInterface $httpClient = null) + { + $this->httpClient = $httpClient; + + return $this; + } + /** * @return Payum */ @@ -198,9 +329,10 @@ public function getPayum() throw new \LogicException('Token storage must be configured.'); } - $genericTokenFactory = $this->buildGenericTokenFactory(); + $tokenFactory = $this->buildTokenFactory($this->tokenStorage, $this->buildRegistry([], $this->storages)); + $genericTokenFactory = $this->buildGenericTokenFactory($tokenFactory); - $httpRequestVerifier = $this->buildHttpRequestVerifier(); + $httpRequestVerifier = $this->buildHttpRequestVerifier($this->tokenStorage); if (false == $httpClient = $this->httpClient) { $httpClient = HttpClientFactory::create(); @@ -225,32 +357,42 @@ public function getPayum() } $registry = $this->buildRegistry($gateways, $this->storages, $gatewayFactories); + + } return new Payum($registry, $httpRequestVerifier, $genericTokenFactory); } /** - * @return GenericTokenFactoryInterface + * @param StorageInterface $tokenStorage + * @param StorageRegistryInterface $storageRegistry + * + * @return TokenFactoryInterface */ - protected function buildGenericTokenFactory() + protected function buildTokenFactory(StorageInterface $tokenStorage, StorageRegistryInterface $storageRegistry) { - if ($this->genericTokenFactory) { - return $this->genericTokenFactory; - } + $tokenFactory = $this->tokenFactory; - $tokenStorage = $this->tokenStorage; - $storageRegistry = $this->buildRegistry([], $this->storages); + if (is_callable($tokenFactory)) { + $tokenFactory = call_user_func($tokenFactory, $tokenStorage, $storageRegistry); - if ($this->tokenFactoryBuilder) { - $tokenFactory = call_user_func($this->tokenFactoryBuilder, $tokenStorage, $storageRegistry); - } else if ($this->tokenFactory) { - $tokenFactory = $this->tokenFactory; - } else { - $tokenFactory = new TokenFactory($tokenStorage, $storageRegistry); + if (false == $tokenFactory instanceof TokenFactoryInterface) { + throw new \LogicException('Builder returned invalid instance'); + } } - return new GenericTokenFactory($tokenFactory, $this->genericTokenFactoryPaths ?: [ + return $tokenFactory ?: new TokenFactory($tokenStorage, $storageRegistry); + } + + /** + * @param TokenFactoryInterface $tokenFactory + * + * @return GenericTokenFactoryInterface + */ + protected function buildGenericTokenFactory(TokenFactoryInterface $tokenFactory) + { + return $this->genericTokenFactory ?: new GenericTokenFactory($tokenFactory, $this->genericTokenFactoryPaths ?: [ 'capture' => 'capture.php', 'notify' => 'notify.php', 'authorize' => 'authorize.php', @@ -259,60 +401,73 @@ protected function buildGenericTokenFactory() } /** - * @param array $gateways - * @param array $storages - * @param array $gatewayFactories + * @param StorageInterface $tokenStorage * - * @return RegistryInterface + * @return HttpRequestVerifierInterface */ - protected function buildRegistry(array $gateways = [], array $storages = [], array $gatewayFactories = []) + private function buildHttpRequestVerifier(StorageInterface $tokenStorage) { - $fallbackRegistry = new SimpleRegistry($gateways, $storages, $gatewayFactories); - if ($this->gatewayConfigStorage) { - $fallbackRegistry = new DynamicRegistry($this->gatewayConfigStorage, $fallbackRegistry); - } + $httpRequestVerifier = $this->httpRequestVerifier; - if ($this->mainRegistry) { - $registry = new FallbackRegistry($this->mainRegistry, $fallbackRegistry); - } else { - $registry = $fallbackRegistry; + if (is_callable($httpRequestVerifier)) { + $httpRequestVerifier = call_user_func($httpRequestVerifier, $tokenStorage); + + if (false == $httpRequestVerifier instanceof HttpRequestVerifierInterface) { + throw new \LogicException('Builder returned invalid instance'); + } } - return $registry; + return $httpRequestVerifier ?: new HttpRequestVerifier($tokenStorage); } /** - * @return HttpRequestVerifierInterface + * @param array $config + * @return GatewayFactoryInterface */ - private function buildHttpRequestVerifier() + private function buildCoreGatewayFactory(array $config) { - if ($this->httpRequestVerifier) { - return $this->httpRequestVerifier; + $coreGatewayFactory = $this->coreGatewayFactory; + + foreach ($this->storages as $modelClass => $storage) { + $extensionName = 'payum.extension.storage_'.strtolower(str_replace('\\', '_', $modelClass)); + + $config[$extensionName] = new StorageExtension($storage); } - if ($this->httpRequestVerifierBuilder) { - return call_user_func($this->httpRequestVerifierBuilder, $this->tokenStorage); + if (is_callable($coreGatewayFactory)) { + $coreGatewayFactory = call_user_func($coreGatewayFactory, $config); + + if (false == $coreGatewayFactory instanceof GatewayFactoryInterface) { + throw new \LogicException('Builder returned invalid instance'); + } } - return new HttpRequestVerifier($this->tokenStorage); + return $coreGatewayFactory ?: new CoreGatewayFactory($config); } /** - * @param array $defaultConfig + * @param array $gateways + * @param array $storages + * @param array $gatewayFactories * - * @return GatewayFactoryInterface + * @return RegistryInterface */ - private function buildCoreGatewayFactory(array $defaultConfig) + protected function buildRegistry(array $gateways = [], array $storages = [], array $gatewayFactories = []) { - if ($this->coreGatewayFactory) { - return $this->coreGatewayFactory; + $fallbackRegistry = new SimpleRegistry($gateways, $storages, $gatewayFactories); + $fallbackRegistry->setAddStorageExtensions(false); + + if ($this->gatewayConfigStorage) { + $fallbackRegistry = new DynamicRegistry($this->gatewayConfigStorage, $fallbackRegistry); } - if ($this->coreGatewayFactoryBuilder) { - return call_user_func($this->coreGatewayFactoryBuilder, $defaultConfig); + if ($this->mainRegistry) { + $registry = new FallbackRegistry($this->mainRegistry, $fallbackRegistry); + } else { + $registry = $fallbackRegistry; } - return new CoreGatewayFactory($defaultConfig); + return $registry; } /** @@ -370,160 +525,4 @@ protected function buildGatewayFactories(GatewayFactoryInterface $coreGatewayFac return array_replace($defaultGatewayFactories, $gatewayFactories); } - - /** - * @param HttpRequestVerifierInterface $httpRequestVerifier - * - * @return static - */ - public function setHttpRequestVerifier(HttpRequestVerifierInterface $httpRequestVerifier = null) - { - $this->httpRequestVerifier = $httpRequestVerifier; - - return $this; - } - - /** - * @param TokenFactoryInterface $tokenFactory - * - * @return static - */ - public function setTokenFactory(TokenFactoryInterface $tokenFactory = null) - { - $this->tokenFactory = $tokenFactory; - - return $this; - } - - /** - * @param callable $builder - * - * @return static - */ - public function setTokenFactoryBuilder(callable $builder = null) - { - $this->tokenFactoryBuilder = $builder; - - return $this; - } - - /** - * @param GenericTokenFactoryInterface $tokenFactory - * - * @return static - */ - public function setGenericTokenFactory(GenericTokenFactoryInterface $tokenFactory = null) - { - $this->genericTokenFactory = $tokenFactory; - - return $this; - } - - /** - * @param \string[] $paths - * - * @return static - */ - public function setGenericTokenFactoryPaths(array $paths = []) - { - $this->genericTokenFactoryPaths = $paths; - - return $this; - } - - /** - * @param StorageInterface $tokenStorage - * - * @return static - */ - public function setTokenStorage(StorageInterface $tokenStorage = null) - { - $this->tokenStorage = $tokenStorage; - - return $this; - } - - /** - * @param GatewayFactoryInterface $coreGatewayFactory - * - * @return static - */ - public function setCoreGatewayFactory(GatewayFactoryInterface $coreGatewayFactory = null) - { - $this->coreGatewayFactory = $coreGatewayFactory; - - return $this; - } - - /** - * @param array $config - * - * @return static - */ - public function setCoreGatewayFactoryConfig(array $config = null) - { - $this->coreGatewayFactoryConfig = $config; - - return $this; - } - - /** - * @param StorageInterface $gatewayConfigStorage - * - * @return static - */ - public function setGatewayConfigStorage(StorageInterface $gatewayConfigStorage = null) - { - $this->gatewayConfigStorage = $gatewayConfigStorage; - - return $this; - } - - /** - * @param HttpClientInterface $httpClient - * - * @return static - */ - public function setHttpClient(HttpClientInterface $httpClient = null) - { - $this->httpClient = $httpClient; - - return $this; - } - - /** - * @param RegistryInterface $mainRegistry - * - * @return static - */ - public function setMainRegistry(RegistryInterface $mainRegistry = null) - { - $this->mainRegistry = $mainRegistry; - - return $this; - } - - /** - * @param callable $builder - * - * @return static - */ - public function setHttpRequestVerifierBuilder(callable $builder = null) - { - $this->httpRequestVerifierBuilder = $builder; - - return $this; - } - - /** - * @param callable $builder - * - * @return static - */ - public function setCoreGatewayFactoryBuilder(callable $builder = null) - { - $this->coreGatewayFactoryBuilder = $builder; - - return $this; - } } \ No newline at end of file