| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the league/oauth2-client library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Alex Bilbie <[email protected]> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
* @link http://thephpleague.com/oauth2-client/ Documentation |
| 12 |
* @link https://packagist.org/packages/league/oauth2-client Packagist |
| 13 |
* @link https://github.com/thephpleague/oauth2-client GitHub |
| 14 |
*/ |
| 15 |
namespace YoastSEO_Vendor\League\OAuth2\Client\Grant; |
| 16 |
|
| 17 |
use YoastSEO_Vendor\League\OAuth2\Client\Grant\Exception\InvalidGrantException; |
| 18 |
/** |
| 19 |
* Represents a factory used when retrieving an authorization grant type. |
| 20 |
*/ |
| 21 |
class GrantFactory |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected $registry = []; |
| 27 |
/** |
| 28 |
* Defines a grant singleton in the registry. |
| 29 |
* |
| 30 |
* @param string $name |
| 31 |
* @param AbstractGrant $grant |
| 32 |
* @return self |
| 33 |
*/ |
| 34 |
public function setGrant($name, \YoastSEO_Vendor\League\OAuth2\Client\Grant\AbstractGrant $grant) |
| 35 |
{ |
| 36 |
$this->registry[$name] = $grant; |
| 37 |
return $this; |
| 38 |
} |
| 39 |
/** |
| 40 |
* Returns a grant singleton by name. |
| 41 |
* |
| 42 |
* If the grant has not be registered, a default grant will be loaded. |
| 43 |
* |
| 44 |
* @param string $name |
| 45 |
* @return AbstractGrant |
| 46 |
*/ |
| 47 |
public function getGrant($name) |
| 48 |
{ |
| 49 |
if (empty($this->registry[$name])) { |
| 50 |
$this->registerDefaultGrant($name); |
| 51 |
} |
| 52 |
return $this->registry[$name]; |
| 53 |
} |
| 54 |
/** |
| 55 |
* Registers a default grant singleton by name. |
| 56 |
* |
| 57 |
* @param string $name |
| 58 |
* @return self |
| 59 |
*/ |
| 60 |
protected function registerDefaultGrant($name) |
| 61 |
{ |
| 62 |
// PascalCase the grant. E.g: 'authorization_code' becomes 'AuthorizationCode' |
| 63 |
$class = \str_replace(' ', '', \ucwords(\str_replace(['-', '_'], ' ', $name))); |
| 64 |
$class = 'YoastSEO_Vendor\\League\\OAuth2\\Client\\Grant\\' . $class; |
| 65 |
$this->checkGrant($class); |
| 66 |
return $this->setGrant($name, new $class()); |
| 67 |
} |
| 68 |
/** |
| 69 |
* Determines if a variable is a valid grant. |
| 70 |
* |
| 71 |
* @param mixed $class |
| 72 |
* @return boolean |
| 73 |
*/ |
| 74 |
public function isGrant($class) |
| 75 |
{ |
| 76 |
return \is_subclass_of($class, \YoastSEO_Vendor\League\OAuth2\Client\Grant\AbstractGrant::class); |
| 77 |
} |
| 78 |
/** |
| 79 |
* Checks if a variable is a valid grant. |
| 80 |
* |
| 81 |
* @throws InvalidGrantException |
| 82 |
* @param mixed $class |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function checkGrant($class) |
| 86 |
{ |
| 87 |
if (!$this->isGrant($class)) { |
| 88 |
throw new \YoastSEO_Vendor\League\OAuth2\Client\Grant\Exception\InvalidGrantException(\sprintf('Grant "%s" must extend AbstractGrant', \is_object($class) ? \get_class($class) : $class)); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|