<commit_before><?php namespace Codesleeve\Stapler\Storage;

use Aws\S3\S3Client;
use Codesleeve\Stapler\Attachment;

class S3 implements StorageInterface
{
	/**
	 * The current attachedFile object being processed.
	 *
	 * @var \Codesleeve\Stapler\Attachment
	 */
	public $attachedFile;

    /**
     * The AWS S3Client instance.
     *
     * @var S3Client
     */
    protected $s3Client;

	/**
	 * Boolean flag indicating if this attachment's bucket currently exists.
	 *
	 * @var array
	 */
	protected $bucketExists = false;

	/**
	 * Constructor method
	 *
	 * @param \Codesleeve\Stapler\Attachment $attachedFile
     * @param S3Client $s3Client
	 */
	function __construct(Attachment $attachedFile, S3Client $s3Client)
	{
		$this->attachedFile = $attachedFile;
        $this->s3Client = $s3Client;
	}

	/**
	 * Return the url for a file upload.
	 *
	 * @param  string $styleName
	 * @return string
	 */
	public function url($styleName)
	{
		return $this->s3Client->getObjectUrl($this->getBucket(), $this->path($styleName));
	}

	/**
	 * Return the key the uploaded file object is stored under within a bucket.
	 *
	 * @param  string $styleName
	 * @return string
	 */
	public function path($styleName)
	{
		return $this->attachedFile->getInterpolator()->interpolate($this->attachedFile->path, $this->attachedFile, $styleName);
	}

	/**
	 * Remove an attached file.
	 *
	 * @param  array $filePaths
	 * @return void
	 */
	public function remove($filePaths)
	{
		if ($filePaths) {
			$this->s3Client->deleteObjects(['Bucket' => $this->getBucket(), 'Objects' => $this->getKeys($filePaths)]);
		}
	}

	/**
	 * Move an uploaded file to it's intended destination.
	 *
	 * @param  \Codesleeve\Stapler\File\UploadedFile $file
	 * @param  string $filePath
	 * @return void
	 */
	public function move($file, $filePath)
	{
 		$objectConfig = $this->attachedFile->s3_object_config;
 	    $fileSpecificConfig = ['Key' => $filePath, 'SourceFile' => $file, 'ContentType' => $this->attachedFile->contentType()];
 	    $mergedConfig = array_merge($objectConfig, $fileSpecificConfig);
 	    
 	    $this->ensureBucketExists($mergedConfig['Bucket']);
 	    $this->s3Client->putObject($mergedConfig);
	}

	/**
	 * Return an array of paths (bucket keys) for an attachment.
	 * There will be one path for each of the attachmetn's styles.
	 *
	 * @param  $filePaths
	 * @return array
	 */<commit_msg>Fix bug when referencing s3 bucket.<commit_after><?php namespace Codesleeve\Stapler\Storage;

use Aws\S3\S3Client;
use Codesleeve\Stapler\Attachment;

class S3 implements StorageInterface
{
	/**
	 * The current attachedFile object being processed.
	 *
	 * @var \Codesleeve\Stapler\Attachment
	 */
	public $attachedFile;

    /**
     * The AWS S3Client instance.
     *
     * @var S3Client
     */
    protected $s3Client;

	/**
	 * Boolean flag indicating if this attachment's bucket currently exists.
	 *
	 * @var array
	 */
	protected $bucketExists = false;

	/**
	 * Constructor method
	 *
	 * @param \Codesleeve\Stapler\Attachment $attachedFile
     * @param S3Client $s3Client
	 */
	function __construct(Attachment $attachedFile, S3Client $s3Client)
	{
		$this->attachedFile = $attachedFile;
        $this->s3Client = $s3Client;
	}

	/**
	 * Return the url for a file upload.
	 *
	 * @param  string $styleName
	 * @return string
	 */
	public function url($styleName)
	{
		return $this->s3Client->getObjectUrl($this->attachedFile->s3_object_config['Bucket'], $this->path($styleName));
	}

	/**
	 * Return the key the uploaded file object is stored under within a bucket.
	 *
	 * @param  string $styleName
	 * @return string
	 */
	public function path($styleName)
	{
		return $this->attachedFile->getInterpolator()->interpolate($this->attachedFile->path, $this->attachedFile, $styleName);
	}

	/**
	 * Remove an attached file.
	 *
	 * @param  array $filePaths
	 * @return void
	 */
	public function remove($filePaths)
	{
		if ($filePaths) {
			$this->s3Client->deleteObjects(['Bucket' => $this->attachedFile->s3_object_config['Bucket'], 'Objects' => $this->getKeys($filePaths)]);
		}
	}

	/**
	 * Move an uploaded file to it's intended destination.
	 *
	 * @param  \Codesleeve\Stapler\File\UploadedFile $file
	 * @param  string $filePath
	 * @return void
	 */
	public function move($file, $filePath)
	{
 		$objectConfig = $this->attachedFile->s3_object_config;
 	    $fileSpecificConfig = ['Key' => $filePath, 'SourceFile' => $file, 'ContentType' => $this->attachedFile->contentType()];
 	    $mergedConfig = array_merge($objectConfig, $fileSpecificConfig);
 	    
 	    $this->ensureBucketExists($mergedConfig['Bucket']);
 	    $this->s3Client->putObject($mergedConfig);
	}

	/**
	 * Return an array of paths (bucket keys) for an attachment.
	 * There will be one path for each of the attachmetn's styles.
	 *
	 * @param  $filePaths
	 * @return array
	 */
{'fraction_non_alphanumeric': 0.13940520446096655, 'fraction_numerical': 0.007228418009087154, 'mean_word_length': 3.5388940955951265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7643830', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1534, 'n_words': 365}