matecat /
simple-s3
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the Simple S3 package. |
||
| 4 | * |
||
| 5 | * (c) Mauro Cassani<https://github.com/mauretto78> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | * |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Matecat\SimpleS3\Commands\Handlers; |
||
| 13 | |||
| 14 | use Aws\ResultInterface; |
||
| 15 | use Aws\S3\Exception\S3Exception; |
||
| 16 | use Exception; |
||
| 17 | use Matecat\SimpleS3\Commands\CommandHandler; |
||
| 18 | |||
| 19 | class GetItem extends CommandHandler |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Get the details of an item. |
||
| 23 | * For a complete reference: |
||
| 24 | * https://docs.aws.amazon.com/cli/latest/reference/s3api/get-object.html |
||
| 25 | * |
||
| 26 | * @param array $params |
||
| 27 | * |
||
| 28 | * @return ResultInterface|mixed |
||
| 29 | * @throws Exception |
||
| 30 | */ |
||
| 31 | 9 | public function handle(array $params = []): mixed |
|
| 32 | { |
||
| 33 | 9 | $bucketName = $params[ 'bucket' ]; |
|
| 34 | 9 | $keyName = $params[ 'key' ]; |
|
| 35 | 9 | $version = (isset($params[ 'version' ])) ? $params[ 'version' ] : null; |
|
| 36 | |||
| 37 | 9 | if ($this->client->hasEncoder()) { |
|
| 38 | 9 | $keyName = $this->client->getEncoder()->encode($keyName); |
|
| 39 | } |
||
| 40 | |||
| 41 | 9 | if ($this->client->hasCache() and $this->client->getCache()->has($bucketName, $keyName, $version)) { |
|
| 42 | 9 | return $this->returnItemFromCache($bucketName, $keyName, $version); |
|
| 43 | } |
||
| 44 | |||
| 45 | return $this->returnItemFromS3($bucketName, $keyName, $version); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param array $params |
||
| 50 | * |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | 9 | public function validateParams(array $params = []): bool |
|
| 54 | { |
||
| 55 | 9 | return ( |
|
| 56 | 9 | isset($params[ 'bucket' ]) and |
|
| 57 | 9 | isset($params[ 'key' ]) |
|
| 58 | 9 | ); |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $bucketName |
||
| 63 | * @param string $keyName |
||
| 64 | * @param string|null $version |
||
| 65 | * |
||
| 66 | * @return mixed |
||
| 67 | */ |
||
| 68 | 9 | private function returnItemFromCache(string $bucketName, string $keyName, ?string $version = null): mixed |
|
| 69 | { |
||
| 70 | 9 | if ('' === $this->client->getCache()->get($bucketName, $keyName, $version)) { |
|
| 71 | 6 | $config = [ |
|
| 72 | 6 | 'Bucket' => $bucketName, |
|
| 73 | 6 | 'Key' => $keyName |
|
| 74 | 6 | ]; |
|
| 75 | |||
| 76 | 6 | if (null != $version) { |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 77 | 2 | $config[ 'VersionId' ] = $version; |
|
| 78 | } |
||
| 79 | |||
| 80 | 6 | $file = $this->client->getConn()->getObject($config); |
|
| 81 | 6 | $this->client->getCache()->set($bucketName, $keyName, $file->toArray(), $version); |
|
| 82 | } |
||
| 83 | |||
| 84 | 9 | return $this->client->getCache()->get($bucketName, $keyName, $version); |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $bucketName |
||
| 89 | * @param string $keyName |
||
| 90 | * @param string|null $version |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | private function returnItemFromS3(string $bucketName, string $keyName, ?string $version = null): array |
||
| 95 | { |
||
| 96 | try { |
||
| 97 | $config = [ |
||
| 98 | 'Bucket' => $bucketName, |
||
| 99 | 'Key' => $keyName |
||
| 100 | ]; |
||
| 101 | |||
| 102 | if (null != $version) { |
||
|
0 ignored issues
–
show
|
|||
| 103 | $config[ 'VersionId' ] = $version; |
||
| 104 | } |
||
| 105 | |||
| 106 | $file = $this->client->getConn()->getObject($config); |
||
| 107 | |||
| 108 | if ($this->client->hasCache()) { |
||
| 109 | $this->client->getCache()->set($bucketName, $keyName, $file->toArray(), $version); |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->commandHandlerLogger?->log($this, sprintf('File \'%s\' was successfully obtained from \'%s\' bucket', $keyName, $bucketName)); |
||
| 113 | |||
| 114 | return $file->toArray(); |
||
| 115 | } catch (S3Exception $e) { |
||
| 116 | $this->commandHandlerLogger?->logExceptionAndReturnFalse($e); |
||
| 117 | |||
| 118 | throw $e; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 |