<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Storage_Service_Scheme_Dynamic implements Storage_Service_Scheme_Interface
{
  public function generate(array $params)
  {
    if( empty($params['parent_type']) ) {
      throw new Storage_Model_Exception('Unspecified resource parent type');
    } else if( empty($params['file_id']) || !is_numeric($params['file_id']) ) {
      throw new Storage_Model_Exception('Unspecified resource identifier');
    } else if( empty($params['extension']) ) {
      throw new Storage_Model_Exception('Unspecified resource extension');
    }

    extract($params);
    
    $path = 'public' . '/';
    $path .= $parent_type . '/';
    $base = 255;
    $tmp = $file_id;
    
    // Generate subdirs while id > $base
    do {
      $mod = ( $tmp % $base );
      $tmp -= $mod;
      $tmp /= $base;
      $path .= sprintf("%02x", $mod) . '/';
    } while( $tmp > 0 );

    $path .= md5(time() . $hash)
      . '.' . $extension;

    return $path;
  }
}
