Passed
Push — master ( 225a9b...122a16 )
by Daniel
01:45
created

ModifiesLabels::removeLabel()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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 $service;
14
15
	public function __construct()
16
	{
17
		$this->messageRequest = new Google_Service_Gmail_ModifyMessageRequest();
18
	}
19
20
	/**
21
	 * Adds labels to the email
22
	 *
23
	 * @param string|array $labels
24
	 *
25
	 * @return Mail|string
26
	 * @throws \Exception
27
	 */
28
	public function addLabel( $labels )
29
	{
30
		if(is_string($labels)) {
31
			$labels = [$labels];
32
		}
33
34
		$this->messageRequest->setAddLabelIds( $labels );
35
36
		try {
37
			return $this->modify();
38
		} catch ( \Exception $e ) {
39
			throw new \Exception( "Couldn't add labels: {$e->getMessage()}" );
40
		}
41
	}
42
43
	/**
44
	 * Removes labels from the email
45
	 *
46
	 * @param string|array $labels
47
	 *
48
	 * @return Mail|string
49
	 * @throws \Exception
50
	 */
51
	public function removeLabel( $labels )
52
	{
53
		if(is_string($labels)) {
54
			$labels = [$labels];
55
		}
56
57
		$this->messageRequest->setRemoveLabelIds( $labels );
58
59
		try {
60
			return $this->modify();
61
		} catch ( \Exception $e ) {
62
			throw new \Exception( "Couldn't remove labels: {$e->getMessage()}" );
63
		}
64
	}
65
66
	/**
67
	 * Executes the modification
68
	 *
69
	 * @return Mail
70
	 */
71
	private function modify()
72
	{
73
		return new Mail( $this->service->users_messages->modify( 'me', $this->getId(), $this->messageRequest ) );
0 ignored issues
show
Bug introduced by
It seems like getId() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
		return new Mail( $this->service->users_messages->modify( 'me', $this->/** @scrutinizer ignore-call */ getId(), $this->messageRequest ) );
Loading history...
74
	}
75
}