Issues (20)

src/Services/Message/Attachment.php (1 issue)

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