Skip to content

Commit

Permalink
rename ZIP to QIT_CLI\Woo\ZipValidator. Move to namespace Woo a…
Browse files Browse the repository at this point in the history
…nd make it a service
  • Loading branch information
rcstr committed May 6, 2024
1 parent b8178c8 commit 1c68d32
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
17 changes: 13 additions & 4 deletions src/src/Commands/WooValidateZipCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

namespace QIT_CLI\Commands;

use QIT_CLI\Zip;
use QIT_CLI\Woo\ZipValidator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;

class WooValidateZipCommand extends Command {
protected static $defaultName = 'woo:validate-zip'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

private $zip_validator;

public function __construct( ZipValidator $zip_validator ) {
$this->zip_validator = $zip_validator;

parent::__construct();
}

protected function configure() {
$this
->setDescription( 'Validate a local ZIP file\'s content.' )
Expand All @@ -22,11 +30,12 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
try {
$zip_file = $input->getArgument( 'path' );

ZIP::validate_zip( $zip_file );
$this->zip_validator->validate_zip( $zip_file );

$output->writeln( '<info>Zip file content is valid.</info>' );
} catch ( \Exception $e ) {
$output->writeln( sprintf( '<error>An error occurred while validating the ZIP file. Error: %s</error>', $e->getMessage() ) );
$output->writeln( sprintf( '<error>An error occurred while validating the ZIP file. Error: %s</error>',
$e->getMessage() ) );

return Command::FAILURE;
}
Expand Down
22 changes: 11 additions & 11 deletions src/src/Zip.php → src/src/Woo/ZipValidator.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace QIT_CLI;
namespace QIT_CLI\Woo;

use ZipArchive;

class Zip {
class ZipValidator {

/**
* @var string[]
*/
protected static $system_files = [
protected $system_files = [
// https://github.com/github/gitignore/blob/main/Global/Windows.gitignore.
'Thumbs.db',
'Thumbs.db:encryptable',
Expand All @@ -36,7 +36,7 @@ class Zip {
* @return \ZipArchive
* @throws \RuntimeException If the file is not a valid zip file.
*/
private static function open_file( string $filepath ) {
private function open_file( string $filepath ) {
$zip = new ZipArchive();
$opened = $zip->open( $filepath, ZipArchive::CHECKCONS );

Expand All @@ -53,11 +53,11 @@ private static function open_file( string $filepath ) {
* @return void
* @throws \Exception If the zip file is invalid.
*/
public static function validate_zip( string $filepath ) {
$zip = self::open_file( $filepath );
public function validate_zip( string $filepath ) {
$zip = $this->open_file( $filepath );

// Example (foo => foo/).
$plugin_slug = self::extract_slug_from_filepath( $filepath );
$plugin_slug = $this->extract_slug_from_filepath( $filepath );
$parent_dir = strtolower( trim( trim( $plugin_slug ), '/' ) . '/' );

$found_parent_directory = false;
Expand All @@ -77,7 +77,7 @@ public static function validate_zip( string $filepath ) {
continue;
}

if ( self::is_file_invalid( $info['name'] ) ) {
if ( $this->is_file_invalid( $info['name'] ) ) {
throw new \RuntimeException( sprintf( 'Invalid (%s) file/folder inside zip file', $info['name'] ), 400 );
}

Expand All @@ -98,7 +98,7 @@ public static function validate_zip( string $filepath ) {
*
* @return string
*/
private static function extract_slug_from_filepath( string $filepath ): string {
private function extract_slug_from_filepath( string $filepath ): string {
$filename = basename( $filepath );

return substr( $filename, 0, strpos( $filename, '.' ) );
Expand All @@ -109,8 +109,8 @@ private static function extract_slug_from_filepath( string $filepath ): string {
*
* @return bool
*/
protected static function is_file_invalid( string $name ): bool {
foreach ( self::$system_files as $system_file ) {
protected function is_file_invalid( string $name ): bool {
foreach ( $this->system_files as $system_file ) {
if ( str_ends_with( $name, $system_file ) ) {
return true;
}
Expand Down
11 changes: 5 additions & 6 deletions src/tests/ZipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace QIT_CLI_Tests;

use QIT_CLI\App;
use QIT_CLI\Zip;

use QIT_CLI\Woo\ZipValidator;
use QIT_CLI_Tests\ZipBuilderHelper as ZipBuilder;

class ZipTest extends QITTestCase {
Expand Down Expand Up @@ -38,7 +37,7 @@ public function test_valid_zip() {
$file_name = 'valid-zip.zip';
$slug = 'valid-zip';

$zip = App::make( Zip::class );
$zip = App::make( ZipValidator::class );
$zip_file = ( new ZipBuilder( $file_name ) )
->with_file( __DIR__ . '/plugin-entrypoint.php', $slug . '/plugin-entrypoint.php' )
->build();
Expand All @@ -58,7 +57,7 @@ public function test_zip_with_invalid_file() {
$file_name = 'zip-with-invalid-file.zip';
$slug = 'zip-with-invalid-file';

$zip = App::make( Zip::class );
$zip = App::make( ZipValidator::class );
$zip_file = ( new ZipBuilder( $file_name ) )
->with_file( __DIR__ . '/plugin-entrypoint.php', $slug . '/plugin-entrypoint.php' )
->with_file( __DIR__ . '/Thumbs.db', $slug . '/Thumbs.db' )
Expand All @@ -76,7 +75,7 @@ function test_zip_with_invalid_file_inside_folder() {
$file_name = 'zip-with-invalid-file-inside-folder.zip';
$slug = 'zip-with-invalid-file-inside-folder';

$zip = App::make( Zip::class );
$zip = App::make( ZipValidator::class );
$zip_file = ( new ZipBuilder( $file_name ) )
->with_file( __DIR__ . '/plugin-entrypoint.php', $slug . '/plugin-entrypoint.php' )
->with_file( __DIR__ . '/Thumbs.db', $slug . '/foo/Thumbs.db' )
Expand All @@ -94,7 +93,7 @@ function test_zip_with_invalid_file_inside_vendor_folder() {
$file_name = 'zip-with-invalid-file-inside-vendor-folder.zip';
$slug = 'zip-with-invalid-file-inside-vendor-folder';

$zip = App::make( Zip::class );
$zip = App::make( ZipValidator::class );
$zip_file = ( new ZipBuilder( $file_name ) )
->with_file( __DIR__ . '/plugin-entrypoint.php', $slug . '/plugin-entrypoint.php' )
->with_file( __DIR__ . '/Thumbs.db', $slug . '/vendor/Thumbs.db' )
Expand Down

0 comments on commit 1c68d32

Please sign in to comment.