friendship ended with social-app. php is my new best friend
1<?php 2/** 3 * Class OAuthProviderFactory 4 * 5 * @created 25.03.2024 6 * @author smiley <smiley@chillerlan.net> 7 * @copyright 2024 smiley 8 * @license MIT 9 */ 10declare(strict_types=1); 11 12namespace chillerlan\OAuth; 13 14use chillerlan\OAuth\Core\{OAuth1Interface, OAuth2Interface, OAuthInterface}; 15use chillerlan\OAuth\Providers\ProviderException; 16use chillerlan\OAuth\Storage\{MemoryStorage, OAuthStorageInterface}; 17use chillerlan\Settings\SettingsContainerInterface; 18use Psr\Http\Client\ClientInterface; 19use Psr\Http\Message\{RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface}; 20use Psr\Log\{LoggerInterface, NullLogger}; 21use function class_exists; 22 23/** 24 * A simple OAuth provider factory (not sure if that clears the mess...) 25 */ 26class OAuthProviderFactory{ 27 28 protected ClientInterface $http; 29 protected RequestFactoryInterface $requestFactory; 30 protected StreamFactoryInterface $streamFactory; 31 protected UriFactoryInterface $uriFactory; 32 protected LoggerInterface $logger; 33 34 /** 35 * thank you PHP-FIG for absolutely nothing 36 */ 37 public function __construct( 38 ClientInterface $http, 39 RequestFactoryInterface $requestFactory, 40 StreamFactoryInterface $streamFactory, 41 UriFactoryInterface $uriFactory, 42 LoggerInterface $logger = new NullLogger, 43 ){ 44 $this->http = $http; 45 $this->requestFactory = $requestFactory; 46 $this->streamFactory = $streamFactory; 47 $this->uriFactory = $uriFactory; 48 $this->logger = $logger; 49 } 50 51 /** 52 * invokes a provider instance with the given $options and $storage interfaces 53 */ 54 public function getProvider( 55 string $providerFQN, 56 SettingsContainerInterface|OAuthOptions $options = new OAuthOptions, 57 OAuthStorageInterface $storage = new MemoryStorage, 58 ):OAuthInterface|OAuth1Interface|OAuth2Interface{ 59 60 if(!class_exists($providerFQN)){ 61 throw new ProviderException('invalid provider class given'); 62 } 63 64 return new $providerFQN( 65 $options, 66 $this->http, 67 $this->requestFactory, 68 $this->streamFactory, 69 $this->uriFactory, 70 $storage, 71 $this->logger, 72 ); 73 74 } 75 76 /** @codeCoverageIgnore */ 77 public function setLogger(LoggerInterface $logger):static{ 78 $this->logger = $logger; 79 80 return $this; 81 } 82 83 /* 84 * factory getters (convenience) 85 */ 86 87 /** @codeCoverageIgnore */ 88 public function getRequestFactory():RequestFactoryInterface{ 89 return $this->requestFactory; 90 } 91 92 /** @codeCoverageIgnore */ 93 public function getStreamFactory():StreamFactoryInterface{ 94 return $this->streamFactory; 95 } 96 97 /** @codeCoverageIgnore */ 98 public function getUriFactory():UriFactoryInterface{ 99 return $this->uriFactory; 100 } 101 102}