autoaction/vbn-php-client

Pacote para geração de URLs assinadas do GCP CDN.

v1.0.1 2025-05-12 18:52 UTC

This package is auto-updated.

Last update: 2025-05-12 18:53:40 UTC


README

Inspirado em DSL(Domain Specific Language) internas, para controle e solicitação fluída

Abaixo um exemplo prático de como o cliente deve se comportar

Para tests

Subir os containers

make up

Executar testes em versões PHP 5.6 e PHP 7.1

make test

Fase de configuração

<?php

namespace Core\Usbi\Libs;

use AutoAction\VBNAutoAction\Response\CollectionResponses;
use AutoAction\VBNAutoAction\VBNAutoAction;
use Throwable;
use GuzzleHttp\Psr7\Response;

class VBNHelper extends VBNAutoAction
{
    const VBN_PROD_URL = 'https://url_da_cdn';
    const GOOGLE_LOAD_BALANCE = 'nome_do_load_balance';
    const DEFAULT_BUCKET = '';

    /**
    * Inicializa a configuração com as chaves e valores necessários.
    *
    * @return array
    * 
    * A função retorna um array associativo com os seguintes itens:
    * 
    * - 'cdn_key_name' (string): Nome da chave criada no balanceador de carga.
    * - 'cdn_key_value' (string): Valor da chave criada no balanceador de carga.
    * - 'google_key_file_path' (string): Caminho para o arquivo com as credenciais do Google Cloud Platform (GCP).
    * - 'google_bucket_mapped' (array): Lista de nomes de buckets mapeados no balanceador de carga.
    */
    private static function initConfig() {
        return [
            'cdn_key_name' => nome_da_key_criado_no_load_balance,
            'cdn_key_value' => valor_da_key_criado_no_load_balance,
            'google_key_file_path' => arquivo_com_as_credenciais_do_gcp,
            'google_bucket_mapped' => array_com_nomes_dos_bucket_mapeados_no_load_balance,
        ];
    }

    /**
     * @param string $bucket
     * @param string $prefix
     * @return string
     * @throws Throwable
     */
    public static function getOneSingleImage(string $bucketName, string $prefix)
    {
        self::init(self::initConfig());
        $imagePath = "/{$bucketName}/{$prefix}";
        return self::getOneImage($imagePath);
    }

    /**
     * @param string $bucket
     * @param string $prefix
     * @return \AutoAction\VBNAutoAction\Response\CollectionResponses
     * @throws Throwable
     */
    public static function getImagesByPrefix(string $bucketName, string $prefix): CollectionResponses
    {
        self::init(self::initConfig());
        self::setBucket($bucketName);
        self::getGcpCredentials();
        if (!self::$credentials) {
            $response = new Response(200, [], []);
            return new CollectionResponses($response);
        }

        $storage = new StorageClient([
            'credentials' => self::$credentials
        ]);
        $bucket = $storage->bucket($bucketName);
        $objects = $bucket->objects([
            'prefix' => $prefix
        ]);

        try {
            $images = [];
            foreach ($objects as $object) {
                $images[] = "{$bucketName}/{$object->name()}";
            }
            return self::getMultiplesImage($images);
        } catch (\Exception $e) {
            $error = [[
                'fileName' => '',
                'message' => $e->getMessage()
            ]];
            $response = new Response(200, [], json_encode($error));
            return new CollectionResponses($response);
        }
    }

    /**
     * @param array $images
     * @return \AutoAction\VBNAutoAction\Response\CollectionResponses
     * @throws Throwable
     */
    public static function getMultiplesImage($images)
    {
        self::init(self::initConfig());
        return self::getMultiplesImageSignature($images);
    }
}