Skip to content

Commit

Permalink
Update feature added if available
Browse files Browse the repository at this point in the history
  • Loading branch information
emrecanmuslumdns committed Jan 8, 2021
1 parent 0b6774b commit 315bf28
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 105 deletions.
76 changes: 48 additions & 28 deletions src/NotificationTransport/Connection.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EmrecanMuslu\Messenger\Transport\NotificationTransport;

use App\Message\NotificationSmsMessage;
use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Result as DriverResult;
Expand All @@ -31,12 +21,6 @@
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Contracts\Service\ResetInterface;

/**
* @internal since Symfony 5.1
*
* @author Vincent Touzet <[email protected]>
* @author Kévin Dunglas <[email protected]>
*/
class Connection implements ResetInterface
{
protected const TABLE_OPTION_NAME = '_symfony_messenger_table_name';
Expand Down Expand Up @@ -134,12 +118,16 @@ public function send(Envelope $envelope, string $encodedMessage, array $headers,
$notificationSmsMessage = $envelope->getMessage()->getMessage();
$notificationSmsTemplate = $notificationSmsMessage->getTemplate();
$notificationSmsOrderDetail = $notificationSmsMessage->getOrderDetails();

$orderId = $notificationSmsOrderDetail->getId();
$template_name = $this->configuration['template_name'];
$now = new \DateTime();
$availableAt = (clone $now)->modify(sprintf('+%d seconds', $delay / 1000));

$queryBuilder = $this->driverConnection->createQueryBuilder()
->insert($this->configuration['table_name'])
$checkRowId = $this->createQueryBuilderCheckRow($orderId, $template_name);
$queryBuilder = $this->driverConnection->createQueryBuilder();

if(null === $checkRowId){
$queryBuilder->insert($this->configuration['table_name'])
->values([
'order_id' => '?',
'encoded_message' => '?',
Expand All @@ -152,30 +140,44 @@ public function send(Envelope $envelope, string $encodedMessage, array $headers,
'created_at' => '?',
'available_at' => '?',
]);
}else{
$queryBuilder->update($this->configuration['table_name'])
->where("id = $checkRowId")
->set('order_id','?')
->set('encoded_message','?')
->set('headers','?')
->set('provider_name','?')
->set('template_name','?')
->set('handled','?')
->set('created_at','?')
->set('available_at','?');
}


$this->executeStatement($queryBuilder->getSQL(), [
$notificationSmsOrderDetail->getId(),

$queryBuilder->setParameters([
$orderId,
$encodedMessage,
json_encode($headers),
$this->configuration['provider_name'],
$this->configuration['template_name'],
null,
null,
$template_name,
0,
$now,
$availableAt,
$availableAt
], [
null,
null,
null,
null,
null,
null,
null,
null,
Types::DATETIME_MUTABLE,
Types::DATETIME_MUTABLE,
Types::DATETIME_MUTABLE
]);

$sql = $queryBuilder->getSQL();

$this->executeStatement($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes());

return $this->driverConnection->lastInsertId();
}
Expand Down Expand Up @@ -372,6 +374,24 @@ private function createQueryBuilder(): QueryBuilder
->from($this->configuration['table_name'], 'm');
}

private function createQueryBuilderCheckRow(int $orderId, string $template_name): int
{
$queryBuilder = $this->driverConnection->createQueryBuilder()
->select('*')
->from($this->configuration['table_name'])
->where('order_id = ?')
->andWhere('template_name = ?')
->setParameters([
$orderId,
$template_name,
])
->setMaxResults(1);

$result = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes());

return $result instanceof Result || $result instanceof DriverResult ? $result->fetchOne() : $result->fetchColumn();
}

private function executeQuery(string $sql, array $parameters = [], array $types = [])
{
try {
Expand Down
12 changes: 0 additions & 12 deletions src/NotificationTransport/DoctrineReceivedStamp.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EmrecanMuslu\Messenger\Transport\NotificationTransport;

use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;

/**
* @author Vincent Touzet <[email protected]>
*/
class DoctrineReceivedStamp implements NonSendableStampInterface
{
private $id;
Expand Down
12 changes: 0 additions & 12 deletions src/NotificationTransport/DoctrineReceiver.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EmrecanMuslu\Messenger\Transport\NotificationTransport;

use Doctrine\DBAL\DBALException;
Expand All @@ -24,9 +15,6 @@
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;

/**
* @author Vincent Touzet <[email protected]>
*/
class DoctrineReceiver implements ReceiverInterface, MessageCountAwareInterface, ListableReceiverInterface
{
private const MAX_RETRIES = 3;
Expand Down
13 changes: 0 additions & 13 deletions src/NotificationTransport/DoctrineSender.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EmrecanMuslu\Messenger\Transport\NotificationTransport;

use Doctrine\DBAL\DBALException;
Expand All @@ -20,10 +11,6 @@
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;


/**
* @author Vincent Touzet <[email protected]>
*/
class DoctrineSender implements SenderInterface
{
private $connection;
Expand Down
12 changes: 0 additions & 12 deletions src/NotificationTransport/DoctrineTransport.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EmrecanMuslu\Messenger\Transport\NotificationTransport;

use Doctrine\DBAL\Connection as DbalConnection;
Expand All @@ -21,9 +12,6 @@
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

/**
* @author Vincent Touzet <[email protected]>
*/
class DoctrineTransport implements TransportInterface, SetupableTransportInterface, MessageCountAwareInterface, ListableReceiverInterface
{
private $connection;
Expand Down
12 changes: 0 additions & 12 deletions src/NotificationTransport/DoctrineTransportFactory.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EmrecanMuslu\Messenger\Transport\NotificationTransport;

use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
Expand All @@ -20,9 +11,6 @@
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

/**
* @author Vincent Touzet <[email protected]>
*/
class DoctrineTransportFactory implements TransportFactoryInterface
{
private $registry;
Expand Down
16 changes: 0 additions & 16 deletions src/NotificationTransport/PostgreSqlConnection.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EmrecanMuslu\Messenger\Transport\NotificationTransport;

use Doctrine\DBAL\Schema\Table;

/**
* Uses PostgreSQL LISTEN/NOTIFY to push messages to workers.
*
* @internal
*
* @author Kévin Dunglas <[email protected]>
*/
final class PostgreSqlConnection extends Connection
{
/**
Expand Down

0 comments on commit 315bf28

Please sign in to comment.