Skip to content

Commit

Permalink
Merge pull request #2 from AppsDevTeam/2.x
Browse files Browse the repository at this point in the history
Změna flush na queryBuilder
  • Loading branch information
thorewi authored Feb 6, 2022
2 parents 1978f76 + 9f728a4 commit eed43e0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@
composer require adt/doctrine-session-handler
```

Use `columns={<column_name>}` according to your naming strategy in `uniqueConstraints` definition.

```php
<?php

namespace App\Entity;

use ADT\DoctrineSessionHandler\Traits\Session;
use ADT\DoctrineSessionHandler\SessionInterface;
use ADT\DoctrineSessionHandler\SessionTrait;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(
* name="session_storage",
* uniqueConstraints={
* @ORM\UniqueConstraint(name="sessionId", columns={"sessionId"})
* @ORM\UniqueConstraint(columns={"sessionId"})
* }
* )
* @ORM\Entity
*/
class SessionStorage extends BaseEntity {
class SessionStorage extends BaseEntity implements SessionInterface {

use Session;
use SessionTrait;

/**
* @var integer
Expand Down
54 changes: 41 additions & 13 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ADT\DoctrineSessionHandler;

use ADT\DoctrineSessionHandler\Traits\Session;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;

class Handler implements \SessionHandlerInterface
Expand Down Expand Up @@ -30,9 +31,13 @@ public function close(): bool
*/
public function destroy(string $id): bool
{
if ($session = $this->getSession($id)) {
$this->em->remove($session);
$this->em->flush($session);
if ($this->getSession($id) !== null) {
$this->em->createQueryBuilder()
->delete($this->entityClass, "e")
->andWhere('e.sessionId = :id')
->setParameter('id', $id)
->getQuery()
->execute();
}

return TRUE;
Expand Down Expand Up @@ -81,18 +86,36 @@ public function write(string $id, string $data): bool
$expiration = $lifetime ? ($lifetime / 60) : 15;

if (!$session) {
$session = new $this->entityClass;
$session->createdAt = new \DateTime;
$session->sessionId = $id;

$this->em->persist($session);
$metadata = $this->em->getClassMetadata($this->entityClass);

$this->em->getConnection()->createQueryBuilder()
->insert($this->getTableName(), "e")
->values(
[
$metadata->getColumnName('createdAt') => '?',
$metadata->getColumnName('expiresAt') => '?',
$metadata->getColumnName('sessionId') => '?',
$metadata->getColumnName('data') => '?'
]
)
->setParameter(0, (new \DateTime()), Types::DATETIME_MUTABLE)
->setParameter(1, (new \DateTime("+$expiration minutes")), Types::DATETIME_MUTABLE)
->setParameter(2, $id)
->setParameter(3, $data)
->execute();
} else {
$this->em->createQueryBuilder()
->update($this->entityClass, "e")
->set("e.expiresAt", '?1')
->set("e.data", '?2')
->where("e.sessionId = :sessionId")
->setParameter(1, new \DateTime("+$expiration minutes"))
->setParameter(2, $data)
->setParameter("sessionId", $id)
->getQuery()
->execute();
}

$session->expiresAt = new \DateTime("+$expiration minutes");
$session->data = $data;

$this->em->flush($session);

return TRUE;
}

Expand All @@ -109,4 +132,9 @@ protected function getSession(string $id): ?SessionInterface
->getQuery()
->getOneOrNullResult();
}

private function getTableName(): string
{
return $this->em->getClassMetadata($this->entityClass)->getTableName();
}
}
9 changes: 4 additions & 5 deletions src/SessionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@

namespace ADT\DoctrineSessionHandler;

use ADT\DoctrineSessionHandler\SessionInterface;
use Doctrine\ORM\Mapping as ORM;

trait SessionTrait
{
/**
* @ORM\Column(name="sessionId", type="string", nullable=false)
* @ORM\Column(type="string", nullable=false)
*/
public string $sessionId;

/**
* @ORM\Column(name="createdAt", type="datetime", nullable=false)
* @ORM\Column(type="datetime", nullable=false)
*/
public \DateTime $createdAt;

/**
* @ORM\Column(name="expires_at", type="datetime", nullable=false)
* @ORM\Column(type="datetime", nullable=false)
*/
public \DateTime $expiresAt;

/**
* @ORM\Column(name="data", type="text", nullable=true)
* @ORM\Column(type="text", nullable=true)
*/
public string $data;

Expand Down

0 comments on commit eed43e0

Please # to comment.