Skip to content

Commit

Permalink
Adds a PdoResolver, easier to use than PdoStatementResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed Sep 3, 2019
1 parent 827113f commit b9e4dc3
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/Identifier/Resolver/PdoResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Phauthentic (https://github.com/Phauthentic)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic)
* @link https://github.com/Phauthentic
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Phauthentic\Authentication\Identifier\Resolver;

use ArrayAccess;
use ArrayObject;
use PDO;
use PDOException;
use PDOStatement;
use Phauthentic\Authentication\Identifier\Resolver\ResolverInterface;

/**
* PDO Resolver
*/
class PdoResolver implements ResolverInterface
{
/**
* @var \PDO
*/
protected $pdo;

/**
* @var string
*/
protected $sql;

/**
* Constructor.
*
* @param \PDO PDO Instance
* @param string $sql SQL String
*/
public function __construct(PDO $pdo, string $sql)
{
$this->pdo = $pdo;
$this->sql = $sql;
}

/**
* Builds the statement
*
* @return \PDOStatement
*/
protected function buildStatement(): PDOStatement
{
$statement = $this->pdo->prepare($this->sql);

$error = $this->pdo->errorInfo();
if ($error[0] !== '00000') {
throw new PDOException($error[2], (int)$error[0]);
}

return $statement;
}

/**
* {@inheritDoc}
*/
public function find(array $conditions): ?ArrayAccess
{
foreach ($conditions as $key => $value) {
unset($conditions[$key]);
$conditions[':' . $key] = $value;
}

$statement = $this->buildStatement();
$statement->execute($conditions);
$result = $statement->fetchAll();

if (empty($result)) {
return null;
}

return new ArrayObject($result[0]);
}
}
39 changes: 39 additions & 0 deletions tests/TestCase/Identifier/Resolver/PdoResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright (c) Phauthentic (https://github.com/Phauthentic)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic)
* @link https://github.com/Phauthentic
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Phauthentic\Authentication\Test\TestCase\Identifier\Resolver;

use ArrayObject;
use Phauthentic\Authentication\Identifier\Resolver\PdoResolver;
use Phauthentic\Authentication\Test\TestCase\AuthenticationTestCase as TestCase;

/**
* PDO Resolver Test
*/
class PdoResolverTest extends TestCase
{
/**
* testFindWithSuccess
*
* @return void
*/
public function testFindWithSuccess(): void
{
$pdo = static::getPDO();

$resolver = new PdoResolver($pdo, 'SELECT * FROM users WHERE username = :username');
$result = $resolver->find(['username' => 'florian']);

$this->assertInstanceOf(ArrayObject::class, $result);
$this->assertEquals($result['username'], 'florian');
}
}

0 comments on commit b9e4dc3

Please sign in to comment.