Passed
Push — master ( f11bbd...b7bea7 )
by Daniel
03:08
created

HasLabels::createLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
use Google_Service_Gmail;
6
7
trait HasLabels
8
{
9
	/**
10
	 * List the labels in the user's mailbox.
11
	 *
12
	 * @param $userEmail
13
	 *
14
	 * @return \Google\Service\Gmail\ListLabelsResponse
15
	 */
16
	public function labelsList($userEmail)
17
	{
18
		$service = new Google_Service_Gmail($this);
0 ignored issues
show
Bug introduced by
$this of type Dacastro4\LaravelGmail\Traits\HasLabels is incompatible with the type Google\Client|array expected by parameter $clientOrConfig of Google_Service_Gmail::__construct(). ( Ignorable by Annotation )

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

18
		$service = new Google_Service_Gmail(/** @scrutinizer ignore-type */ $this);
Loading history...
19
20
		return $service->users_labels->listUsersLabels($userEmail);
21
	}
22
23
	/**
24
	 * Create new label by name.
25
	 *
26
	 * @param $userEmail
27
	 * @param $label
28
	 *
29
	 * @return \Google\Service\Gmail\Label
30
	 */
31
	public function createLabel($userEmail, $label)
32
	{
33
		$service = new Google_Service_Gmail($this);
0 ignored issues
show
Bug introduced by
$this of type Dacastro4\LaravelGmail\Traits\HasLabels is incompatible with the type Google\Client|array expected by parameter $clientOrConfig of Google_Service_Gmail::__construct(). ( Ignorable by Annotation )

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

33
		$service = new Google_Service_Gmail(/** @scrutinizer ignore-type */ $this);
Loading history...
34
35
		return $service->users_labels->create($userEmail, $label);
36
	}
37
38
	/**
39
	 * first or create label in the user's mailbox.
40
	 *
41
	 * @param $userEmail
42
	 * @param $nLabel
43
	 * @return \Google\Service\Gmail\Label
44
	 */
45
	public function firstOrCreateLabel($userEmail, $newLabel)
46
	{
47
		$labels = $this->labelsList($userEmail);
48
49
		foreach ($labels->getLabels() as $existLabel) {
50
			if ($existLabel->getName() == $newLabel->getName()) {
51
				return $existLabel;
52
			}
53
		}
54
55
		$service = new Google_Service_Gmail($this);
0 ignored issues
show
Bug introduced by
$this of type Dacastro4\LaravelGmail\Traits\HasLabels is incompatible with the type Google\Client|array expected by parameter $clientOrConfig of Google_Service_Gmail::__construct(). ( Ignorable by Annotation )

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

55
		$service = new Google_Service_Gmail(/** @scrutinizer ignore-type */ $this);
Loading history...
56
57
		return $service->users_labels->create($userEmail, $newLabel);
58
	}
59
}