Completed
Push — master ( e6cd79...15515b )
by Mike
02:12
created

File   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 56
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setupDestiantion() 0 9 3
A extractFileName() 0 8 3
A getFileName() 0 3 1
A writeFile() 0 5 1
A file() 0 3 1
1
<?php
2
3
namespace SugarAPI\SDK\Response;
4
5
use SugarAPI\SDK\Response\Abstracts\AbstractResponse;
6
7
class File extends AbstractResponse {
8
9
    /**
10
     * @var
11
     */
12
    protected $fileName;
13
14
    /**
15
     * File Path for response
16
     * @var string
17
     */
18
    protected $destinationPath;
19
20
    public function __construct($curlResponse, $curlRequest,$destination = null) {
21
        parent::__construct($curlResponse, $curlRequest);
22
        $this->extractFileName();
23
        if (!empty($destination)) {
24
            $this->setupDestiantion($destination);
25
            $this->writeFile();
26
        }
27
    }
28
29
    protected function setupDestiantion($destination = null){
30
        if (empty($destination)){
31
            $destination = sys_get_temp_dir().'/SugarAPI';
32
            if (!file_exists($destination)){
33
                mkdir($destination,0777);
34
            }
35
        }
36
        $this->destinationPath = $destination;
37
    }
38
39
    protected function extractFileName(){
40
        foreach (explode("\r\n",$this->headers) as $header)
41
        {
42
            if (strpos($header,'filename')!==FALSE){
43
                $this->fileName = substr($header,(strpos($header,"\"")+1),-1);
44
            }
45
        }
46
    }
47
48
    public function getFileName(){
49
        return $this->fileName;
50
    }
51
52
    protected function writeFile(){
53
        $fileHandle = fopen($this->file(),'w+');
54
        fwrite($fileHandle,$this->body);
55
        fclose($fileHandle);
56
    }
57
58
    public function file(){
59
        return rtrim($this->destinationPath,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$this->fileName;
60
    }
61
62
}