PHP Classes

File: src/Chronicle/Handlers/Lookup.php

Recommend this page to a friend!
  Classes of Scott Arciszewski  >  Chronicle  >  src/Chronicle/Handlers/Lookup.php  >  Download  
File: src/Chronicle/Handlers/Lookup.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Chronicle
Append arbitrary data to a storage container
Author: By
Last change:
Date: 3 years ago
Size: 6,566 bytes
 

Contents

Class file image Download
<?php
namespace ParagonIE\Chronicle\Handlers;

use
ParagonIE\Chronicle\{
   
Chronicle,
   
Exception\HashNotFound,
   
HandlerInterface
};
use
Psr\Http\Message\{
   
RequestInterface,
   
ResponseInterface
};

/**
 * Class Lookup
 * @package ParagonIE\Chronicle\Handlers
 */
class Lookup implements HandlerInterface
{
   
/** @var string */
   
protected $method = 'index';

   
/**
     * Lookup constructor.
     * @param string $method
     */
   
public function __construct(string $method = 'index')
    {
       
$this->method = $method;
    }

   
/**
     * The handler gets invoked by the router. This accepts a Request
     * and returns a Response.
     *
     * @param RequestInterface $request
     * @param ResponseInterface $response
     * @param array $args
     * @return mixed
     */
   
public function __invoke(
       
RequestInterface $request,
       
ResponseInterface $response,
        array
$args = []
    ):
ResponseInterface {
        try {
           
// Whitelist of acceptable methods:
           
switch ($this->method) {
                case
'export':
                    return
$this->exportChain();
                case
'lasthash':
                    return
$this->getLastHash();
                case
'hash':
                    if (!empty(
$args['hash'])) {
                        return
$this->getByHash($args);
                    }
                    break;
                case
'since':
                    if (!empty(
$args['hash'])) {
                        return
$this->getSince($args);
                    }
                    break;
            }
        } catch (\
Throwable $ex) {
            return
Chronicle::errorResponse($response, $ex->getMessage());
        }
        return
Chronicle::errorResponse($response, 'Unknown method: '.$this->method);
    }

   
/**
     * Gets the entire Blakechain.
     *
     * @return ResponseInterface
     */
   
public function exportChain(): ResponseInterface
   
{
        return
Chronicle::getSapient()->createSignedJsonResponse(
           
200,
            [
               
'version' => Chronicle::VERSION,
               
'datetime' => (new \DateTime())->format(\DateTime::ATOM),
               
'status' => 'OK',
               
'results' => $this->getFullChain()
            ],
           
Chronicle::getSigningKey()
        );
    }

   
/**
     * Get information about a particular entry, given its hash.
     *
     * @param array $args
     * @return ResponseInterface
     * @throws HashNotFound
     */
   
public function getByHash(array $args = []): ResponseInterface
   
{
       
$record = Chronicle::getDatabase()->run(
           
"SELECT
                 data AS contents,
                 prevhash,
                 currhash,
                 summaryhash,
                 created,
                 publickey,
                 signature
             FROM
                 chronicle_chain
             WHERE
                 currhash = ?
                 OR summaryhash = ?
            "
,
           
$args['hash'],
           
$args['hash']
        );
        if (!
$record) {
            throw new
HashNotFound('No record found matching this hash.');
        }
        return
Chronicle::getSapient()->createSignedJsonResponse(
           
200,
            [
               
'version' => Chronicle::VERSION,
               
'datetime' => (new \DateTime())->format(\DateTime::ATOM),
               
'status' => 'OK',
               
'results' => $record
           
],
           
Chronicle::getSigningKey()
        );
    }

   
/**
     * List the latest current hash and summary hash
     *
     * @return ResponseInterface
     */
   
public function getLastHash(): ResponseInterface
   
{
       
$lasthash = Chronicle::getDatabase()->row(
           
'SELECT currhash, summaryhash FROM chronicle_chain ORDER BY id DESC LIMIT 1'
       
);
        return
Chronicle::getSapient()->createSignedJsonResponse(
           
200,
            [
               
'version' => Chronicle::VERSION,
               
'datetime' => (new \DateTime())->format(\DateTime::ATOM),
               
'status' => 'OK',
               
'results' => [
                   
'current-hash' =>
                       
$lasthash['currhash'],
                   
'summary-hash' =>
                       
$lasthash['summaryhash']
                ]
            ],
           
Chronicle::getSigningKey()
        );
    }

   
/**
     * Get updates to the chain since a given hash
     *
     * @param array $args
     * @return ResponseInterface
     * @throws HashNotFound
     */
   
public function getSince(array $args = []): ResponseInterface
   
{
       
$id = Chronicle::getDatabase()->cell(
           
"SELECT
                 id
             FROM
                 chronicle_chain
             WHERE
                 currhash = ?
                 OR summaryhash = ?
             ORDER BY id ASC
            "
,
           
$args['hash'],
           
$args['hash']
        );
        if (!
$id) {
            throw new
HashNotFound('No record found matching this hash.');
        }
       
$since = Chronicle::getDatabase()->run(
           
"SELECT
                 data AS contents,
                 prevhash,
                 currhash,
                 summaryhash,
                 created,
                 publickey,
                 signature
             FROM
                 chronicle_chain
             WHERE
                 id > ?
            "
,
           
$id
       
);

        return
Chronicle::getSapient()->createSignedJsonResponse(
           
200,
            [
               
'version' => Chronicle::VERSION,
               
'datetime' => (new \DateTime())->format(\DateTime::ATOM),
               
'status' => 'OK',
               
'results' => $since
           
],
           
Chronicle::getSigningKey()
        );
    }

   
/**
     * Get the entire chain, as-is, as of the time of the request.
     *
     * @return array
     */
   
protected function getFullChain(): array
    {
       
$chain = [];
       
$rows = Chronicle::getDatabase()->run("SELECT * FROM chronicle_chain ORDER BY id ASC");
        foreach (
$rows as $row) {
           
$chain[] = [
               
'contents' => $row['data'],
               
'prev' => $row['prevhash'],
               
'hash' => $row['currhash'],
               
'summary' => $row['summaryhash'],
               
'created' => $row['created'],
               
'publickey' => $row['publickey'],
               
'signature' => $row['signature']
            ];
        }
        return
$chain;
    }
}

For more information send a message to info at phpclasses dot org.