Author: Shivam Mathur (shivammathur) Date: 2024-08-12T07:21:29+05:30 Commit: https://github.com/php/web-downloads/commit/290b67d5d49cf253f7be8f86ba9773a5423dd303 Raw diff: https://github.com/php/web-downloads/commit/290b67d5d49cf253f7be8f86ba9773a5423dd303.diff
Add HandlerInterface and BaseHandler Changed paths: A src/BaseHandler.php A src/HandlerInterface.php M src/IndexHandler.php M src/PeclHandler.php M src/PhpHandler.php M src/Router.php M src/WinlibsHandler.php Diff: diff --git a/src/BaseHandler.php b/src/BaseHandler.php new file mode 100644 index 0000000..b6a7961 --- /dev/null +++ b/src/BaseHandler.php @@ -0,0 +1,16 @@ +<?php + +namespace App; + +abstract class BaseHandler implements HandlerInterface +{ + public function handle(): void { + $data = json_decode(file_get_contents('php://input'), true); + + if($this->validate($data)) { + $this->execute($data); + } + } + protected abstract function validate(array $data): bool; + protected abstract function execute(array $data): void; +} \ No newline at end of file diff --git a/src/HandlerInterface.php b/src/HandlerInterface.php new file mode 100644 index 0000000..f2bdacc --- /dev/null +++ b/src/HandlerInterface.php @@ -0,0 +1,8 @@ +<?php + +namespace App; + +interface HandlerInterface +{ + public function handle(): void; +} \ No newline at end of file diff --git a/src/IndexHandler.php b/src/IndexHandler.php index c15db73..62af27b 100644 --- a/src/IndexHandler.php +++ b/src/IndexHandler.php @@ -1,10 +1,20 @@ <?php namespace App; -class IndexHandler +class IndexHandler extends BaseHandler { public function handle(): void { echo 'Welcome!'; } + + protected function validate(array $data): bool + { + return true; + } + + protected function execute(array $data): void + { + // + } } \ No newline at end of file diff --git a/src/PeclHandler.php b/src/PeclHandler.php index 9ed320a..fa114cf 100644 --- a/src/PeclHandler.php +++ b/src/PeclHandler.php @@ -5,19 +5,9 @@ use Exception; use ZipArchive; -class PeclHandler +class PeclHandler extends BaseHandler { - - public function handle(): void - { - $data = json_decode(file_get_contents('php://input'), true); - - if($this->validate($data)) { - $this->execute($data); - } - } - - private function validate(mixed $data): bool + protected function validate(mixed $data): bool { $validator = new Validator([ 'url' => 'required|url', @@ -37,7 +27,7 @@ private function validate(mixed $data): bool return $valid; } - private function execute(array $data): void + protected function execute(array $data): void { try { extract($data); @@ -53,7 +43,8 @@ private function execute(array $data): void */ private function fetchExtension(string $extension, string $ref, string $url, string $token): void { - $filepath = "/tmp/$extension-$ref.zip"; + $filepath = "/tmp/$extension-$ref-" . strtotime('now') . ".zip"; + FetchArtifact::handle($url, $filepath, $token); if(!file_exists($filepath)) { diff --git a/src/PhpHandler.php b/src/PhpHandler.php index 320eb76..617c915 100644 --- a/src/PhpHandler.php +++ b/src/PhpHandler.php @@ -2,10 +2,20 @@ namespace App; -class PhpHandler +class PhpHandler extends BaseHandler { - public function handle() + public function handle(): void { } + + protected function validate(array $data): bool + { + return true; + } + + protected function execute(array $data): void + { + // + } } \ No newline at end of file diff --git a/src/Router.php b/src/Router.php index 7d16d5f..9152eec 100644 --- a/src/Router.php +++ b/src/Router.php @@ -42,6 +42,7 @@ public function handleRequest(): void return; } + /** @var BaseHandler $handler */ $handler = $route['handler']; (new $handler)->handle(); } elseif (!empty($allowedMethods)) { diff --git a/src/WinlibsHandler.php b/src/WinlibsHandler.php index ee0b918..c541f82 100644 --- a/src/WinlibsHandler.php +++ b/src/WinlibsHandler.php @@ -2,10 +2,20 @@ namespace App; -class WinlibsHandler +class WinlibsHandler extends BaseHandler { - public function handle() + public function handle(): void { } + + protected function validate(array $data): bool + { + return true; + } + + protected function execute(array $data): void + { + // + } } \ No newline at end of file