CoreFiles by mySites.guru
StorageBase.php80 lines, 2,130 bytes, from Joomla_5.1.0-rc1-Release_Candidate-Full_Package.zip
Raw History Compare with 5.0.3-rc1
md5
ed98743a7de07e8de59ec5f1843cc408
sha1
06fac88321c5fef74c2f53e7e705b2abbcfbbbf8
sha256
79f20e176f0a9761e50ee291935173025dfdc228340ffe149c7a3caa16e7d34c
<?php

namespace Tuf\Metadata;

/**
 * Defines a base class for saving and loading trusted TUF metadata.
 */
abstract class StorageBase implements StorageInterface
{
    /**
     * Reads raw metadata.
     *
     * @param string $name
     *   The name of the metadata to read (e.g., `root`, `targets`), without
     *   file extension.
     *
     * @return string|null
     *   The metadata, as a string, or null if the metadata is unavailable.
     */
    abstract protected function read(string $name): ?string;

    /**
     * Writes trusted metadata as a string.
     *
     * @param string $name
     *   The name of the metadata to write (e.g., `root`, `targets`), without
     *   file extension.
     * @param string $data
     *   The metadata to write, fully encoded and normalized as desired.
     */
    abstract protected function write(string $name, string $data): void;

    /**
     * {@inheritdoc}
     */
    public function getRoot(): RootMetadata
    {
        $data = $this->read(RootMetadata::TYPE);
        if ($data) {
            return RootMetadata::createFromJson($data)->trust();
        }
        throw new \LogicException("Could not load root metadata.");
    }

    /**
     * {@inheritdoc}
     */
    public function getTimestamp(): ?TimestampMetadata
    {
        $data = $this->read(TimestampMetadata::TYPE);
        return $data ? TimestampMetadata::createFromJson($data)->trust() : null;
    }

    /**
     * {@inheritdoc}
     */
    public function getSnapshot(): ?SnapshotMetadata
    {
        $data = $this->read(SnapshotMetadata::TYPE);
        return $data ? SnapshotMetadata::createFromJson($data)->trust() : null;
    }

    /**
     * {@inheritdoc}
     */
    public function getTargets(string $role = 'targets'): ?TargetsMetadata
    {
        $data = $this->read($role);
        return $data ? TargetsMetadata::createFromJson($data, $role)->trust() : null;
    }

    /**
     * {@inheritdoc}
     */
    public function save(MetadataBase $metadata): void
    {
        $metadata->ensureIsTrusted();
        $this->write($metadata->getRole(), $metadata->getSource());
    }
}

The same content is in 136 releases, Joomla 5.1.0-beta1 to 6.3.0-alpha1. See them all, or every version of this file.