Advertisement
Guest User

S3

a guest
Oct 1st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Helpers;
  4.  
  5. use UnexpectedValueException;
  6. use InvalidArgumentException;
  7. use App\Facades\XmlXsdValidator;
  8. use Illuminate\Support\Facades\Cache;
  9. use Nathanmac\Utilities\Parser\Facades\Parser;
  10.  
  11. class S3
  12. {
  13.     /**
  14.      * Public s3 bucket url.
  15.      *
  16.      * @var
  17.      */
  18.     private $s3BucketUrl;
  19.  
  20.     /**
  21.      * S3 constructor.
  22.      *
  23.      * @throws Exception
  24.      */
  25.     public function __construct()
  26.     {
  27.         // Load s3 bucket url from env config
  28.         $this->s3BucketUrl = config('mvf.s3BucketUrl');
  29.  
  30.         // Check if the s3 bucket url is loaded
  31.         if (empty($this->s3BucketUrl)) {
  32.             throw new InvalidArgumentException('Invalid aws s3 bucket url - check .env file to see if AWS_S3_BUCKET is defined.');
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * Method to list customer files s3 bucket.
  38.      *
  39.      * @return array
  40.      * @throws Exception
  41.      */
  42.     public function listCustomerFiles()
  43.     {
  44.         // Load from cache or live s3 bucket
  45.         return Cache::remember('Customer_Files', config('mvf.cacheMins'), function()
  46.         {
  47.             // Init
  48.             $customerFiles = [];
  49.  
  50.             // Load s3 bucket contents xml from aws
  51.             $bucketDataXml = @file_get_contents($this->s3BucketUrl);
  52.  
  53.             // Check if s3 bucket contents xml was loaded
  54.             if ($bucketDataXml === false) {
  55.                 throw new UnexpectedValueException('Failed to load s3 bucket contents xml - check url: ' . $this->s3BucketUrl);
  56.             }
  57.  
  58.             // Proceed if loaded s bucket contents xml validates against xsd schema
  59.             if (XmlXsdValidator::validate($bucketDataXml, config('mvf.listBucketResultXsdPath')))
  60.             {
  61.                 // Parse s3 bucket content xml
  62.                 $bucketData = Parser::xml($bucketDataXml);
  63.  
  64.                 // Parse bucket contents
  65.                 foreach ($bucketData['Contents'] as $content) {
  66.                     $customerFiles[] = $content['Key'];
  67.                 }
  68.  
  69.                 // Flip file list array into a hash map for faster look-up
  70.                 $customerFiles = array_flip($customerFiles);
  71.             }
  72.  
  73.             // Finished
  74.             return $customerFiles;
  75.         });
  76.     }
  77.  
  78.     /**
  79.      * Method to load customer accounts from customer file in s3 bucket.
  80.      *
  81.      * @param $customerGuid
  82.      * @return mixed
  83.      */
  84.     public function loadCustomerAccounts($customerGuid)
  85.     {
  86.         // Load from cache or live s3 bucket
  87.         return Cache::remember(sprintf('Customer_%s_Accounts', $customerGuid), config('mvf.cacheMins'), function() use ($customerGuid)
  88.         {
  89.             // Init
  90.             $customerAccounts = [];
  91.  
  92.             // Generate file url
  93.             $fileUrl = $this->s3BucketUrl . $customerGuid . '.json';
  94.  
  95.             // Download requested file from s3 bucket
  96.             $customerFileData = file_get_contents($fileUrl);
  97.  
  98.             // Check if the requested file was downloaded from s3 bucket
  99.             if ($customerFileData === false) {
  100.                 throw new UnexpectedValueException('Failed to load requested file from s3 bucket - ' . $fileUrl);
  101.             }
  102.  
  103.             // Decode json file data
  104.             $customerFileData = @json_decode($customerFileData);
  105.             if ($customerFileData === null) {
  106.                 throw new UnexpectedValueException('Failed to decode json data.');
  107.             }
  108.  
  109.             // Parse customer accounts & data
  110.             if (isset($customerFileData->accounts) && is_array($customerFileData->accounts)) {
  111.                 foreach ($customerFileData->accounts as $account) {
  112.                     $customerAccounts[] = $account->id;
  113.                     $accountDataCacheKey = sprintf('Account_%s_Data', $account->id);
  114.                     if (!Cache::has($accountDataCacheKey)) {
  115.                         unset($account->id);
  116.                         Cache::set($accountDataCacheKey, $account, config('mvf.cacheMins'));
  117.                     }
  118.                 }
  119.             }
  120.  
  121.             // Finished
  122.             return $customerAccounts;
  123.         });
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement