rugk/threema-msgapi-sdk-php

View on GitHub
source/Threema/MsgApi/PublicKeyStores/File.php

Summary

Maintainability
A
45 mins
Test Coverage
<?php
/**
 * @author Threema GmbH
 * @copyright Copyright (c) 2015-2016 Threema GmbH
 */

namespace Threema\MsgApi\PublicKeyStores;

use Threema\Core\Exception;
use Threema\MsgApi\PublicKeyStore;

/**
 * Store the PublicKeys in a ascii file
 *
 * @package Threema\MsgApi\PublicKeyStores
 */
class File extends PublicKeyStore {
    /**
     * @var string
     */
    private $file;

    /**
     * @param string $file Valid, read and writable file
     * @throws Exception if the file does not exist or not writable
     */
    public function __construct($file) {
        if(false === is_writable($file)) {
            throw new Exception('file '.$file.' does not exist or is not writable');
        }
        $this->file = $file;
    }

    /**
     * return null if the public key not found in the store
     * @param string $threemaId
     * @return null|string
     * @throws Exception
     */
    public function findPublicKey($threemaId) {
        $storeHandle = fopen($this->file, 'r');
        if(false === $storeHandle) {
            throw new Exception('could not open file '.$this->file);
        }
        else {
            $threemaId = strtoupper($threemaId);
            $publicKey = null;
            while (!feof($storeHandle)) {
                $buffer = fgets($storeHandle, 4096);
                if(substr($buffer, 0, 8) == $threemaId) {
                    $publicKey = str_replace("\n", '', substr($buffer, 8));
                    continue;
                }
                // Process buffer here..
            }
            fclose($storeHandle);
            return $publicKey;
        }
    }

    /**
     * save a public key
     * @param string $threemaId
     * @param string $publicKey
     * @return bool
     */
    public function savePublicKey($threemaId, $publicKey) {
        return file_put_contents($this->file, $threemaId.$publicKey."\n", FILE_APPEND) !== false;
    }

    /**
     * Initialize a new File Public Key Store
     * @param string $path the file will be created if it does not exist
     * @return File
     */
    public static function create($path) {
        if(false === file_exists($path)) {
            //touch
            touch($path);
        }

        return new File($path);
    }
}