<commit_before><?php declare(strict_types=1);

namespace Gos\Bundle\WebSocketBundle\Client;

use Gos\Bundle\WebSocketBundle\Client\Driver\DriverInterface;
use Gos\Bundle\WebSocketBundle\Client\Exception\ClientNotFoundException;
use Gos\Bundle\WebSocketBundle\Client\Exception\StorageException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Ratchet\ConnectionInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
final class ClientStorage implements ClientStorageInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * @var DriverInterface
     */
    private $driver;

    /**
     * @var int
     */
    private $ttl;

    public function __construct(int $ttl)
    {
        $this->ttl = $ttl;
    }

    private function getStorageDriver(): DriverInterface
    {
        if (!$this->driver) {
            throw new \RuntimeException(
                sprintf(
                    'Storage driver not set in "%s". Did you forget to call "%s::setStorageDriver()?',
                    static::class,
                    static::class
                )
            );
        }

        return $this->driver;
    }

    public function setStorageDriver(DriverInterface $driver): void
    {
        $this->driver = $driver;
    }

    public function getClient(string $identifier): TokenInterface
    {
        try {
            $result = $this->getStorageDriver()->fetch($identifier);
        } catch (\Exception $e) {
            throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
        }

        if ($this->logger) {
            $this->logger->debug('GET CLIENT '.$identifier);
        }

        if (false === $result) {
            throw new ClientNotFoundException(sprintf('Client %s not found', $identifier));
        }

        return unserialize($result);
    }

    public function getStorageId(ConnectionInterface $conn): string
    {
        return $conn->resourceId;
    }

    public function addClient(string $identifier, TokenInterface $token): void
    {
        $serializedUser = serialize($token);

        if ($this->logger) {
            $context = [
                'token' => $token,
                'username' => $token->getUsername(),
            ];

            $this->logger->debug('INSERT CLIENT '.$identifier, $context);
        }

        try {
            $result = $this->getStorageDriver()->save($identifier, $serializedUser, $this->ttl);
        } catch (\Exception $e) {
            throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
        }
<commit_msg>Typecast the connection's resourceId property, Ratchet's IoServer casts this to an integer when assigning it but we don't want to force all storage IDs to be integers as a result<commit_after><?php declare(strict_types=1);

namespace Gos\Bundle\WebSocketBundle\Client;

use Gos\Bundle\WebSocketBundle\Client\Driver\DriverInterface;
use Gos\Bundle\WebSocketBundle\Client\Exception\ClientNotFoundException;
use Gos\Bundle\WebSocketBundle\Client\Exception\StorageException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Ratchet\ConnectionInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
final class ClientStorage implements ClientStorageInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * @var DriverInterface
     */
    private $driver;

    /**
     * @var int
     */
    private $ttl;

    public function __construct(int $ttl)
    {
        $this->ttl = $ttl;
    }

    private function getStorageDriver(): DriverInterface
    {
        if (!$this->driver) {
            throw new \RuntimeException(
                sprintf(
                    'Storage driver not set in "%s". Did you forget to call "%s::setStorageDriver()?',
                    static::class,
                    static::class
                )
            );
        }

        return $this->driver;
    }

    public function setStorageDriver(DriverInterface $driver): void
    {
        $this->driver = $driver;
    }

    public function getClient(string $identifier): TokenInterface
    {
        try {
            $result = $this->getStorageDriver()->fetch($identifier);
        } catch (\Exception $e) {
            throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
        }

        if ($this->logger) {
            $this->logger->debug('GET CLIENT '.$identifier);
        }

        if (false === $result) {
            throw new ClientNotFoundException(sprintf('Client %s not found', $identifier));
        }

        return unserialize($result);
    }

    public function getStorageId(ConnectionInterface $conn): string
    {
        return (string) $conn->resourceId;
    }

    public function addClient(string $identifier, TokenInterface $token): void
    {
        $serializedUser = serialize($token);

        if ($this->logger) {
            $context = [
                'token' => $token,
                'username' => $token->getUsername(),
            ];

            $this->logger->debug('INSERT CLIENT '.$identifier, $context);
        }

        try {
            $result = $this->getStorageDriver()->save($identifier, $serializedUser, $this->ttl);
        } catch (\Exception $e) {
            throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
        }

{'fraction_non_alphanumeric': 0.12504517528008674, 'fraction_numerical': 0.0003614022406938923, 'mean_word_length': 2.356579745300182, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1273081', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1461, 'n_words': 340}