From d40c43f5ae873864b9c742e308ec54ace1acb5d5 Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Thu, 18 May 2023 08:40:44 +0200 Subject: [PATCH 1/3] Config --- .gitignore | 1 + composer.json | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 1ac43a1..28b9f56 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ test/coverage # Dev .DS_Store .nova/* +.php-cs-fixer.cache diff --git a/composer.json b/composer.json index 9c2710a..b0ec56e 100644 --- a/composer.json +++ b/composer.json @@ -10,14 +10,16 @@ } ], "require": { - "php": "^8.0", + "php": ">=8.1", "phant/error": "1.*", - "aws/aws-sdk-php": "^3.215" + "aws/aws-sdk-php": "3.*" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.0", "phpstan/phpstan": "^1.4" }, "scripts": { + "lint": "vendor/bin/php-cs-fixer fix ./ --rules=@PSR12", "analyse": "vendor/bin/phpstan analyse component --memory-limit=4G" }, "autoload": { From a9f30d0d21f25261321ac331298023de639e23dc Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Thu, 18 May 2023 08:58:05 +0200 Subject: [PATCH 2/3] MySQL --- README.md | 2 +- component/MySQL.php | 108 ------------------------------- component/Port/MySQL.php | 46 ++++++++++++++ component/Service/MySQL.php | 122 ++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 109 deletions(-) delete mode 100644 component/MySQL.php create mode 100644 component/Port/MySQL.php create mode 100644 component/Service/MySQL.php diff --git a/README.md b/README.md index c10302d..094b506 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ PHP >= 8.0 ### MySQL ```php -use Phant\Client\MySQL as ClientMySQL; +use Phant\Client\Service\MySQL as ClientMySQL; $clientMySQL = new ClientMySQL( '127.0.0.1', diff --git a/component/MySQL.php b/component/MySQL.php deleted file mode 100644 index 14e35b2..0000000 --- a/component/MySQL.php +++ /dev/null @@ -1,108 +0,0 @@ -pdo = new PDO( - 'mysql:host=' . $host . ';dbname=' . $dbname . ';port=' . $port . ';charset=' . $charset, - $user, - $pass, - [ - PDO::ATTR_EMULATE_PREPARES => false, - PDO::MYSQL_ATTR_DIRECT_QUERY => false, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION - ] - ); - } - - public function execute(string $query, array $values = []): void - { - $query = $this->pdo->prepare($query); - $query->execute($values); - } - - public function getList(string $query, array $values = []): array - { - $query = $this->pdo->prepare($query); - $query->execute($values); - $all = $query->fetchAll(); - - if ($query->rowCount() == 0) { - throw new NotFound; - } - - return $all; - } - - public function getListByColumnValue(string $table, string $column, string|int|float|bool $value): array - { - return $this->getList(' - SELECT * - FROM `' . $table . '` - WHERE `' . $column . '` = :value - ', [ - ':value' => $value, - ]); - } - - public function get(string $query, array $values = []): array - { - $query = $this->pdo->prepare($query); - $query->execute($values); - $one = $query->fetch(); - - if ($query->rowCount() == 0) { - throw new NotFound; - } - - return $one; - } - - public function getByColumnValue(string $table, string $column, string|int|float|bool $value): array - { - return $this->get(' - SELECT * - FROM `' . $table . '` - WHERE `' . $column . '` = :value - ', [ - ':value' => $value, - ]); - } - - public function exist(string $query, array $values = []): bool - { - $query = $this->pdo->prepare($query); - $query->execute($values); - $exist = $query->fetch(PDO::FETCH_ASSOC); - - return ($exist != false); - } - - public function existByColumnValue(string $table, string $column, string|int|float|bool $value): bool - { - return $this->exist(' - SELECT * - FROM `' . $table . '` - WHERE `' . $column . '` = :value - ', [ - ':value' => $value, - ]); - } -} diff --git a/component/Port/MySQL.php b/component/Port/MySQL.php new file mode 100644 index 0000000..20525dc --- /dev/null +++ b/component/Port/MySQL.php @@ -0,0 +1,46 @@ + false, + PDO::MYSQL_ATTR_DIRECT_QUERY => false, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION + ] + ) { + $this->pdo = new PDO( + 'mysql:host=' . $host . ';dbname=' . $dbname . ';port=' . $port . ';charset=' . $charset, + $user, + $pass, + $config + ); + } + + public function execute( + string $query, + array $values = [] + ): void { + $statement = $this->pdo->prepare($query); + $statement->execute($values); + } + + public function getList( + string $query, + array $values = [] + ): array { + $statement = $this->pdo->prepare($query); + $statement->execute($values); + $all = $statement->fetchAll(); + + if ($statement->rowCount() == 0) { + throw new NotFound(); + } + + return $all; + } + + public function getListByColumnValue( + string $table, + string $column, + string|int|float|bool $value + ): array { + return $this->getList( + 'SELECT * FROM `' . $table . '` WHERE `' . $column . '` = :value', + [ + ':value' => $value, + ] + ); + } + + public function get( + string $query, + array $values = [] + ): array { + $statement = $this->pdo->prepare($query); + $statement->execute($values); + $one = $statement->fetch(); + + if ($statement->rowCount() == 0) { + throw new NotFound(); + } + + return $one; + } + + public function getByColumnValue( + string $table, + string $column, + string|int|float|bool $value + ): array { + return $this->get( + 'SELECT * FROM `' . $table . '` WHERE `' . $column . '` = :value', + [ + ':value' => $value, + ] + ); + } + + public function exist( + string $query, + array $values = [] + ): bool { + $statement = $this->pdo->prepare($query); + $statement->execute($values); + $exist = $statement->fetch(PDO::FETCH_ASSOC); + + return ($exist != false); + } + + public function existByColumnValue( + string $table, + string $column, + string|int|float|bool $value + ): bool { + return $this->exist( + 'SELECT * FROM `' . $table . '` WHERE `' . $column . '` = :value', + [ + ':value' => $value, + ] + ); + } +} From 28ae47bbb3d927bede18b94b4f1e0b87d02fd7c9 Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Thu, 18 May 2023 08:59:47 +0200 Subject: [PATCH 3/3] S3 --- README.md | 27 ++++++++++-- component/Port/S3.php | 24 +++++++++++ component/Port/S3/Object.php | 21 ++++++++++ component/S3.php | 62 --------------------------- component/Service/S3.php | 74 +++++++++++++++++++++++++++++++++ component/Service/S3/Object.php | 45 ++++++++++++++++++++ 6 files changed, 187 insertions(+), 66 deletions(-) create mode 100644 component/Port/S3.php create mode 100644 component/Port/S3/Object.php delete mode 100644 component/S3.php create mode 100644 component/Service/S3.php create mode 100644 component/Service/S3/Object.php diff --git a/README.md b/README.md index 094b506..32df986 100644 --- a/README.md +++ b/README.md @@ -99,16 +99,35 @@ $exist = $clientMySQL->existByColumnValue( ### S3 ```php -use Phant\Client\S3 as ClientS3; +use Phant\Client\Service\S3 as ClientS3; $clientS3 = new ClientS3( $region, $endpoint, $accessKey, $secretKey, +); +$clientS3->setObject($bucket, 'foo', 'bar'); +$bar = $clientS3->getObject($bucket, 'foo'); +$clientS3->deleteObject($bucket, 'foo'); +``` + +### S3 Bucket + +```php +use Phant\Client\Service\S3 as ClientS3; +use Phant\Client\Service\S3\Object as ClientS3Object; + +$clientS3Object = new ClientS3Object( + new ClientS3( + $region, + $endpoint, + $accessKey, + $secretKey, + ), $bucket ); -$clientS3->set('foo', 'bar'); -$bar = $clientS3->get('foo'); -$clientS3->delete('foo'); +$clientS3Object->set('foo', 'bar'); +$bar = $clientS3Object->get('foo'); +$clientS3Object->delete('foo'); ``` diff --git a/component/Port/S3.php b/component/Port/S3.php new file mode 100644 index 0000000..ac301eb --- /dev/null +++ b/component/Port/S3.php @@ -0,0 +1,24 @@ +S3Client = new S3Client([ - 'region' => $region, - 'endpoint' => $endpoint, - 'credentials' => [ - 'key' => $accessKey, - 'secret' => $secretKey, - ], - 'version' => $version, - 'use_path_style_endpoint' => true - ]); - } - - public function set(string $bucket, string $key, string $body) - { - $this->S3Client->putObject([ - 'Bucket' => $bucket, - 'Key' => $key, - 'Body' => $body, - ]); - } - - public function get(string $bucket, string $key): string - { - try { - $object = $this->S3Client->getObject([ - 'Bucket' => $bucket, - 'Key' => $key, - ]); - - return $object['Body']->getContents(); - } catch (\Exception $e) { - throw new NotFound('Object : ' . $key); - } - } - - public function delete(string $bucket, string $key) - { - try { - $this->S3Client->deleteObject([ - 'Bucket' => $bucket, - 'Key' => $key, - ]); - } catch (\Exception $e) { - throw new NotFound('Object : ' . $key); - } - } -} diff --git a/component/Service/S3.php b/component/Service/S3.php new file mode 100644 index 0000000..77ee9bf --- /dev/null +++ b/component/Service/S3.php @@ -0,0 +1,74 @@ +S3Client = new S3Client([ + 'region' => $region, + 'endpoint' => $endpoint, + 'credentials' => [ + 'key' => $accessKey, + 'secret' => $secretKey, + ], + 'version' => $version, + 'use_path_style_endpoint' => true + ]); + } + + public function setObject( + string $bucket, + string $key, + string $body + ): void { + $this->S3Client->putObject([ + 'Bucket' => $bucket, + 'Key' => $key, + 'Body' => $body, + ]); + } + + public function getObject( + string $bucket, + string $key + ): string { + try { + $object = $this->S3Client->getObject([ + 'Bucket' => $bucket, + 'Key' => $key, + ]); + + return $object['Body']->getContents(); + } catch (\Exception $e) { + throw new NotFound('Object : ' . $key); + } + } + + public function deleteObject( + string $bucket, + string $key + ): void { + try { + $this->S3Client->deleteObject([ + 'Bucket' => $bucket, + 'Key' => $key, + ]); + } catch (\Exception $e) { + throw new NotFound('Object : ' . $key); + } + } +} diff --git a/component/Service/S3/Object.php b/component/Service/S3/Object.php new file mode 100644 index 0000000..e371a37 --- /dev/null +++ b/component/Service/S3/Object.php @@ -0,0 +1,45 @@ +client->setObject( + $this->bucket, + $key, + $body, + ); + } + + public function get( + string $key + ): string { + return $this->client->getObject( + $this->bucket, + $key, + ); + } + + public function delete( + string $key + ): void { + $this->client->deleteObject( + $this->bucket, + $key, + ); + } +}