Passed
Pull Request — master (#85)
by
unknown
04:06
created

Attachment::saveAttachmentTo()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 3
dl 0
loc 24
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Services\Message;
4
5
use Dacastro4\LaravelGmail\GmailConnection;
6
use Dacastro4\LaravelGmail\Traits\HasDecodableBody;
7
use Google_Service_Gmail;
8
use Illuminate\Support\Facades\Storage;
9
10
class Attachment extends GmailConnection
11
{
12
	use HasDecodableBody;
13
14
	/**
15
	 * @var
16
	 */
17
	public $body;
18
	/**
19
	 * @var
20
	 */
21
	public $id;
22
	/**
23
	 * @var
24
	 */
25
	public $filename;
26
	/**
27
	 * @var
28
	 */
29
	public $mimeType;
30
	/**
31
	 * @var
32
	 */
33
	public $size;
34
    /**
35
     * @var
36
     */
37
    public $headerDetails;
38
    /**
39
     * @var
40
     */
41
    private $headers;
0 ignored issues
show
introduced by
The private property $headers is not used, and could be removed.
Loading history...
42
	/**
43
	 * @var Google_Service_Gmail
44
	 */
45
	private $service;
46
47
	/**
48
	 * @var
49
	 */
50
	private $messageId;
51
52
	/**
53
	 * Attachment constructor.
54
	 *
55
	 * @param $singleMessageId
56
	 * @param  \Google_Service_Gmail_MessagePart  $part
57
	 */
58
	public function __construct($singleMessageId, \Google_Service_Gmail_MessagePart $part)
59
	{
60
		parent::__construct(config());
61
62
		$this->service = new Google_Service_Gmail($this);
63
64
		$body = $part->getBody();
65
		$this->id = $body->getAttachmentId();
66
		$this->size = $body->getSize();
67
		$this->filename = $part->getFilename();
68
		$this->mimeType = $part->getMimeType();
69
		$this->messageId = $singleMessageId;
70
        $headers = $part->getHeaders();
71
        $this->headerDetails = $this->getHeaderDetails($headers);
72
	}
73
74
	/**
75
	 * Retuns attachment ID
76
	 *
77
	 * @return string
78
	 */
79
	public function getId()
80
	{
81
		return $this->id;
82
	}
83
84
	/**
85
	 * Returns attachment file name
86
	 *
87
	 * @return string
88
	 */
89
	public function getFileName()
90
	{
91
		return $this->filename;
92
	}
93
94
	/**
95
	 * Returns mime type of the attachment
96
	 *
97
	 * @return string
98
	 */
99
	public function getMimeType()
100
	{
101
		return $this->mimeType;
102
	}
103
104
	/**
105
	 * Returns approximate size of the attachment
106
	 *
107
	 * @return mixed
108
	 */
109
	public function getSize()
110
	{
111
		return $this->size;
112
	}
113
114
	/**
115
	 * @param  string  $path
116
	 * @param  string|null  $filename
117
	 *
118
	 * @param  string  $disk
119
	 *
120
	 * @return string
121
	 * @throws \Exception
122
	 */
123
	public function saveAttachmentTo($path = null, $filename = null, $disk = 'local')
124
	{
125
126
		$data = $this->getDecodedBody($this->getData());
127
128
		if (!$data) {
129
			throw new \Exception('Could not get the attachment.');
130
		}
131
132
		$filename = $filename ?: $this->filename;
133
134
		if (is_null($path)) {
135
			$path = '/';
136
		} else {
137
			if (!ends_with('/', $path)) {
138
				$path = "{$path}/";
139
			}
140
		}
141
142
		$filePathAndName = "{$path}{$filename}";
143
144
		Storage::disk($disk)->put($filePathAndName, $data);
145
146
		return $filePathAndName;
147
148
	}
149
150
	/**
151
	 * @throws \Exception
152
	 */
153
	public function getData()
154
	{
155
		$attachment = $this->service->users_messages_attachments->get('me', $this->messageId, $this->id);
156
157
		return $attachment->getData();
158
	}
159
160
	/**
161
     * Returns attachment headers
162
     * Contains Content-ID and X-Attachment-Id for embedded images
163
     *
164
     * @return array
165
     */
166
    public function getHeaderDetails($headers)
167
    {
168
        $headerDetails = [];
169
        foreach ($headers as $header){
170
            $headerDetails[$header->name] = $header->value;
171
        }
172
        return $headerDetails;
173
    }
174
}
175