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/README.md b/README.md index c10302d..32df986 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', @@ -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/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 @@ +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/MySQL.php b/component/Service/MySQL.php new file mode 100644 index 0000000..12d0adf --- /dev/null +++ b/component/Service/MySQL.php @@ -0,0 +1,122 @@ + 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, + ] + ); + } +} 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, + ); + } +} 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": {