This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sonata.payment.generator.postgres service (#418)
Update docs for sonata.payment.generator.postgres
- Loading branch information
1 parent
0e25bdc
commit 08ef8e1
Showing
5 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\Component\Generator; | ||
|
||
use Sonata\Component\Invoice\InvoiceInterface; | ||
use Sonata\Component\Order\OrderInterface; | ||
use Symfony\Bridge\Doctrine\RegistryInterface; | ||
|
||
final class PostgresReference implements ReferenceInterface | ||
{ | ||
/** | ||
* @var RegistryInterface | ||
*/ | ||
private $registry; | ||
|
||
public function __construct(RegistryInterface $registry) | ||
{ | ||
$this->registry = $registry; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function invoice(InvoiceInterface $invoice) | ||
{ | ||
if (!$invoice->getId()) { | ||
throw new \RuntimeException('The invoice is not persisted into the database'); | ||
} | ||
|
||
$this->generateReference($invoice, | ||
$this->registry->getManager()->getClassMetadata(get_class($invoice))->table['name']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function order(OrderInterface $order) | ||
{ | ||
if (!$order->getId()) { | ||
throw new \RuntimeException('The order is not persisted into the database'); | ||
} | ||
|
||
$this->generateReference($order, | ||
$this->registry->getManager()->getClassMetadata(get_class($order))->table['name']); | ||
} | ||
|
||
/** | ||
* @param mixed $object | ||
* @param string $tableName | ||
* | ||
* @throws \Exception | ||
* | ||
* @return string | ||
*/ | ||
protected function generateReference($object, $tableName) | ||
{ | ||
$date = new \DateTime(); | ||
|
||
$sql = sprintf( | ||
"SELECT count(id) as counter FROM %s WHERE created_at >= '%s' AND reference IS NOT NULL", | ||
$tableName, | ||
$date->format('Y-m-d') | ||
); | ||
|
||
$this->registry->getConnection()->exec(sprintf('BEGIN WORK')); | ||
|
||
$this->registry->getConnection()->exec(sprintf('LOCK TABLE %s IN EXCLUSIVE MODE', $tableName)); | ||
|
||
try { | ||
$statement = $this->registry->getConnection()->query($sql); | ||
$row = $statement->fetch(); | ||
|
||
$reference = sprintf('%02d%02d%02d%06d', | ||
$date->format('y'), | ||
$date->format('n'), | ||
$date->format('j'), | ||
$row['counter'] + 1 | ||
); | ||
|
||
$this->registry->getConnection()->update( | ||
$tableName, | ||
array('reference' => $reference), | ||
array('id' => $object->getId()) | ||
); | ||
$object->setReference($reference); | ||
} catch (\Exception $e) { | ||
$this->registry->getConnection()->exec(sprintf('ROLLBACK WORK')); | ||
|
||
throw $e; | ||
} | ||
|
||
$this->registry->getConnection()->exec(sprintf('COMMIT WORK')); | ||
|
||
return $reference; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\Component\Tests\Generator; | ||
|
||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Sonata\Component\Generator\PostgresReference; | ||
use Sonata\Tests\Helpers\PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* @author Anton Zlotnikov <exp.razor@gmail.com> | ||
*/ | ||
class PostgresReferenceTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testInvoice() | ||
{ | ||
$invoice = new InvoiceMock(); | ||
$postgresReference = $this->generateNewObject(); | ||
|
||
$this->expectException('\RuntimeException', 'The invoice is not persisted into the database'); | ||
$postgresReference->invoice($invoice); | ||
|
||
$invoice->setId(13); | ||
|
||
try { | ||
$this->assertNull($postgresReference->invoice($invoice)); | ||
} catch (\Exception $e) { | ||
$this->fail('->invoice() should return a NULL value but should now throw an \Exception'); | ||
} | ||
} | ||
|
||
public function testOrder() | ||
{ | ||
$order = new OrderMock(); | ||
$postgresReference = $this->generateNewObject(); | ||
|
||
$this->expectException('\RuntimeException', 'The order is not persisted into the database'); | ||
$postgresReference->order($order); | ||
|
||
$order->setId(13); | ||
|
||
try { | ||
$this->assertNull($postgresReference->order($order)); | ||
} catch (\Exception $e) { | ||
$this->fail('->order() should return a NULL value but should not throw an \Exception'); | ||
} | ||
} | ||
|
||
/** | ||
* @return \Sonata\Component\Generator\PostgresReference | ||
*/ | ||
private function generateNewObject() | ||
{ | ||
$metadata = new ClassMetadata('entityName'); | ||
$metadata->table = array('name' => 'tableName'); | ||
|
||
$connection = $this->createMock('Doctrine\DBAL\Connection'); | ||
|
||
$em = $this->createMock('Doctrine\ORM\EntityManager'); | ||
$em->expects($this->any()) | ||
->method('getClassMetadata') | ||
->will($this->returnValue($metadata)); | ||
|
||
$connection->expects($this->any()) | ||
->method('query') | ||
->will($this->returnValue(new \PDOStatement())); | ||
|
||
$registry = $this->createMock('Symfony\Bridge\Doctrine\RegistryInterface'); | ||
$registry->expects($this->any())->method('getManager')->will($this->returnValue($em)); | ||
$registry->expects($this->any())->method('getConnection')->will($this->returnValue($connection)); | ||
|
||
return new PostgresReference($registry); | ||
} | ||
} |