Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #41 from wshafer/allow-existing-pdo
Browse files Browse the repository at this point in the history
Allow existing PDO service to be used
  • Loading branch information
ezimuel committed Jul 16, 2018
2 parents 067fe6d + 0a8abd4 commit 9eee319
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@
/vendor/
/zf-mkdoc-theme.tgz
/zf-mkdoc-theme/
.idea
10 changes: 10 additions & 0 deletions docs/book/intro.md
Expand Up @@ -101,11 +101,21 @@ the `username`, and the `password`, if required. The SQL structure of this
database is stored in the [data/oauth2.sql](https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/data/oauth2.sql)
file.

If you already have a PDO service configured, you can simply pass in the service
name to the `pdo` key as follows:

```php
return [
'pdo' => 'myServiceName',
];
```

The `grants` array is for enabling/disabling grants. By default all the supported
grants are configured to be available. If you would like to disable any of the
supplied grants, simply change the value for the grant to NULL. Additionally,
you can extend this array to add your own custom grants.


You need to provide an OAuth2 database yourself, or generate a [SQLite](https://www.sqlite.org)
database with the following command (using `sqlite3` for GNU/Linux):

Expand Down
4 changes: 2 additions & 2 deletions src/Repository/Pdo/AbstractRepository.php
Expand Up @@ -23,9 +23,9 @@ class AbstractRepository
/**
* Constructor
*
* @param PdoService $pdo
* @param \PDO $pdo
*/
public function __construct(PdoService $pdo)
public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Repository/Pdo/PdoServiceFactory.php
Expand Up @@ -15,7 +15,7 @@

class PdoServiceFactory
{
public function __invoke(ContainerInterface $container) : PdoService
public function __invoke(ContainerInterface $container) : \PDO
{
$config = $container->has('config') ? $container->get('config') : [];
$config = $config['authentication']['pdo'] ?? null;
Expand All @@ -24,6 +24,17 @@ public function __invoke(ContainerInterface $container) : PdoService
'The PDO configuration is missing'
);
}

if (is_string($config) && ! $container->has($config)) {
throw new Exception\InvalidConfigException(
'Invalid service for PDO'
);
}

if (is_string($config) && $container->has($config)) {
return $container->get($config);
}

if (! isset($config['dsn'])) {
throw new Exception\InvalidConfigException(
'The DSN configuration is missing for PDO'
Expand Down
35 changes: 35 additions & 0 deletions test/Repository/Pdo/PdoServiceFactoryTest.php
Expand Up @@ -73,4 +73,39 @@ public function testValidConfigurationResultsInReturnedPdoServiceInstance()

$this->assertInstanceOf(PdoService::class, $pdo);
}

public function testValidServiceInConfigurationReturnsPdoService()
{
$mockPdo = $this->prophesize(\PDO::class);

$this->container->has('config')->willReturn(true);
$this->container->get('config')->willReturn([
'authentication' => [
'pdo' => 'My\Pdo\Service',
],
]);

$this->container->has('My\Pdo\Service')->willReturn(true);
$this->container->get('My\Pdo\Service')->willReturn($mockPdo->reveal());

$pdo = ($this->factory)($this->container->reveal());

$this->assertInstanceOf(\PDO::class, $pdo);
}

public function testRaisesExceptionIfPdoServiceIsInvalid()
{
$this->container->has('config')->willReturn(true);
$this->container->get('config')->willReturn([
'authentication' => [
'pdo' => 'My\Invalid\Service',
],
]);

$this->container->has('My\Invalid\Service')->willReturn(false);

$this->expectException(Exception\InvalidConfigException::class);

($this->factory)($this->container->reveal());
}
}

0 comments on commit 9eee319

Please sign in to comment.