| Conditions | 9 |
| Paths | 12 |
| Total Lines | 75 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | #!/usr/bin/php |
||
| 28 | use SA\CpeSdk; |
||
| 29 | |||
| 30 | class ValidateAssetActivity extends BasicActivity |
||
| 31 | { |
||
| 32 | private $finfo; |
||
|
|
|||
| 33 | private $s3; |
||
| 34 | private $curl_data = ''; |
||
| 35 | |||
| 36 | public function __construct($client = null, $params, $debug, $cpeLogger) |
||
| 37 | { |
||
| 38 | # Check if preper env vars are setup |
||
| 39 | if (!($region = getenv("AWS_DEFAULT_REGION"))) |
||
| 40 | throw new CpeSdk\CpeException("Set 'AWS_DEFAULT_REGION' environment variable!"); |
||
| 41 | |||
| 42 | parent::__construct($client, $params, $debug, $cpeLogger); |
||
| 43 | |||
| 44 | $this->s3 = new \Aws\S3\S3Client([ |
||
| 45 | "version" => "latest", |
||
| 46 | "region" => $region |
||
| 47 | ]); |
||
| 48 | } |
||
| 49 | |||
| 50 | // Used to limit the curl download in case of an HTTP encode |
||
| 51 | private function writefn($ch, $chunk) |
||
| 52 | { |
||
| 53 | static $limit = 1024; // 500 bytes, it's only a test |
||
| 54 | |||
| 55 | $len = strlen($this->curl_data) + strlen($chunk); |
||
| 56 | if ($len >= $limit ) { |
||
| 57 | $this->curl_data .= substr($chunk, 0, $limit-strlen($this->curl_data)); |
||
| 58 | return -1; |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->curl_data .= $chunk; |
||
| 62 | return strlen($chunk); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Perform the activity |
||
| 66 | public function process($task) |
||
| 67 | { |
||
| 68 | $this->cpeLogger->logOut( |
||
| 69 | "INFO", |
||
| 70 | basename(__FILE__), |
||
| 71 | "Preparing Asset validation ...", |
||
| 72 | $this->logKey |
||
| 73 | ); |
||
| 74 | |||
| 75 | // Call parent process: |
||
| 76 | parent::process($task); |
||
| 77 | |||
| 78 | $this->activityHeartbeat(); |
||
| 79 | $tmpFile = tempnam(sys_get_temp_dir(), 'ct'); |
||
| 80 | |||
| 81 | if (isset($this->input->{'input_asset'}->{'http'})) { |
||
| 82 | $ch = curl_init(); |
||
| 83 | curl_setopt($ch, CURLOPT_URL, $this->input->{'input_asset'}->{'http'}); |
||
| 84 | curl_setopt($ch, CURLOPT_RANGE, '0-1024'); |
||
| 85 | curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'writefn')); |
||
| 86 | curl_exec($ch); |
||
| 87 | curl_close($ch); |
||
| 88 | $chunk = $this->curl_data; |
||
| 89 | } |
||
| 90 | else if (isset($this->input->{'input_asset'}->{'bucket'}) && |
||
| 91 | isset($this->input->{'input_asset'}->{'file'})) { |
||
| 92 | // Fetch first 1 KiB of the file for Magic number validation |
||
| 93 | $obj = $this->s3->getObject([ |
||
| 94 | 'Bucket' => $this->input->{'input_asset'}->{'bucket'}, |
||
| 95 | 'Key' => $this->input->{'input_asset'}->{'file'}, |
||
| 96 | 'Range' => 'bytes=0-1024' |
||
| 97 | ]); |
||
| 98 | $chunk = (string) $obj['Body']; |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->activityHeartbeat(); |
||
| 102 | |||
| 103 | // Determine file type |
||
| 256 |
This check marks private properties in classes that are never used. Those properties can be removed.