JFIF  x x C         C     "        } !1AQa "q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz        w !1AQ aq"2B #3Rbr{ gilour

File "Transaction.php"

Full Path: /home/palsarh/web/palsarh.in/public_html/vendor/kreait/firebase-php/src/Firebase/Database/Transaction.php
File size: 2.02 KB
MIME-type: text/x-php
Charset: utf-8

<?php

declare(strict_types=1);

namespace Kreait\Firebase\Database;

use Kreait\Firebase\Exception\Database\ReferenceHasNotBeenSnapshotted;
use Kreait\Firebase\Exception\Database\TransactionFailed;
use Kreait\Firebase\Exception\DatabaseException;

use function array_key_exists;

class Transaction
{
    /**
     * @var array<string, string>
     */
    private array $etags;

    /**
     * @internal
     */
    public function __construct(private readonly ApiClient $apiClient)
    {
        $this->etags = [];
    }

    /**
     * @throws DatabaseException
     */
    public function snapshot(Reference $reference): Snapshot
    {
        $path = $reference->getPath();

        $result = $this->apiClient->getWithETag($path);

        $this->etags[$path] = $result['etag'];

        return new Snapshot($reference, $result['value']);
    }

    /**
     * @throws ReferenceHasNotBeenSnapshotted
     * @throws TransactionFailed
     */
    public function set(Reference $reference, mixed $value): void
    {
        $etag = $this->getEtagForReference($reference);

        try {
            $this->apiClient->setWithEtag($reference->getPath(), $value, $etag);
        } catch (DatabaseException $e) {
            throw TransactionFailed::onReference($reference, $e);
        }
    }

    /**
     * @throws ReferenceHasNotBeenSnapshotted
     * @throws TransactionFailed
     */
    public function remove(Reference $reference): void
    {
        $etag = $this->getEtagForReference($reference);

        try {
            $this->apiClient->removeWithEtag($reference->getPath(), $etag);
        } catch (DatabaseException $e) {
            throw TransactionFailed::onReference($reference, $e);
        }
    }

    /**
     * @throws ReferenceHasNotBeenSnapshotted
     */
    private function getEtagForReference(Reference $reference): string
    {
        $path = $reference->getPath();

        if (array_key_exists($path, $this->etags)) {
            return $this->etags[$path];
        }

        throw new ReferenceHasNotBeenSnapshotted($reference);
    }
}