ModifiesLabels   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 65
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeLabel() 0 12 3
A modify() 0 3 1
A addLabel() 0 12 3
A __construct() 0 3 1
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
use Dacastro4\LaravelGmail\Services\Message\Mail;
6
use Google_Service_Gmail_ModifyMessageRequest;
7
8
trait ModifiesLabels
9
{
10
11
	private $messageRequest;
12
13
	public function __construct()
14
	{
15
		$this->messageRequest = new Google_Service_Gmail_ModifyMessageRequest();
16
	}
17
18
	/**
19
	 * Adds labels to the email
20
	 *
21
	 * @param string|array $labels
22
	 *
23
	 * @return Mail|string
24
	 * @throws \Exception
25
	 */
26
	public function addLabel($labels)
27
	{
28
		if (is_string($labels)) {
29
			$labels = [$labels];
30
		}
31
32
		$this->messageRequest->setAddLabelIds($labels);
33
34
		try {
35
			return $this->modify();
36
		} catch (\Exception $e) {
37
			throw new \Exception("Couldn't add labels: {$e->getMessage()}");
38
		}
39
	}
40
41
	/**
42
	 * Executes the modification
43
	 *
44
	 * @return Mail
45
	 */
46
	private function modify()
47
	{
48
		return new Mail($this->service->users_messages->modify('me', $this->getId(), $this->messageRequest));
49
	}
50
51
	public abstract function getId();
52
53
	/**
54
	 * Removes labels from the email
55
	 *
56
	 * @param string|array $labels
57
	 *
58
	 * @return Mail|string
59
	 * @throws \Exception
60
	 */
61
	public function removeLabel($labels)
62
	{
63
		if (is_string($labels)) {
64
			$labels = [$labels];
65
		}
66
67
		$this->messageRequest->setRemoveLabelIds($labels);
68
69
		try {
70
			return $this->modify();
71
		} catch (\Exception $e) {
72
			throw new \Exception("Couldn't remove labels: {$e->getMessage()}");
73
		}
74
	}
75
}
76