jeudi 2 février 2017

Class contains only static function, which design pattern apply?

I built a class have only static methods, this class basically a URL builder contains 2 public static methods and those methods calls a private methods also in the same class. my question is could this class be re-written to apply a design pattern? what in my mind its closest to facade or decorator! is my approch is correct?

class UrlBuilder{
/**
 * @param Model $model
 * @return string
 */
private static function jsonFyModelAttribute(Model $model){
    return json_encode($model->attributes);
}

private static function objectFy(array $json){
    return json_decode($json['payload']);
}

/**
 * @param $str
 * @return string
 */
private static function buildFormUrl($str){
    $slug = self::encodeUrl($str);
    return $url = Yii::$app->params['formsUrl'].'pay?pay=' . $slug;
}

/**
 * @param Model $model
 * @return string
 */
public static function sign(Model $model){
    $jws = new SimpleJWS([ 'alg' => 'RS256']);
    $payLoad = self::jsonFyModelAttribute($model);
    $jws->setPayload([
        'payload' => $payLoad
    ]);

    $privateKey =    openssl_pkey_get_private("file:///var/pems/mykey.pem");
    $jws->sign($privateKey);
    return self::buildFormUrl($jws->getTokenString());
}

/**
 * @param $str
 * @return array
 */
public static function unSign($str){
    $data = self::decodeUrl($str);
    $jws= SimpleJWS::load($data);
    $publicKey = openssl_pkey_get_public("file:///var/pems/pubkey.pem");
    if ($jws->isValid($publicKey, 'RS256')) {
        return self::objectFy($jws->getPayload());
    }
}

/**
 * @param $str
 * @return string
 */
private static function encodeUrl($str){
    return urlencode($str);
}

/**
 * @param $str
 * @return string
 */
private static function decodeUrl($str){
    return urldecode($str);
}

}

Aucun commentaire:

Enregistrer un commentaire