Completed
Push — master ( 2d3dcc...9dbdcf )
by Daniel
04:02
created

ModifiesLabels::addLabels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 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
	public function __construct()
12
	{
13
		$this->messageRequest = new Google_Service_Gmail_ModifyMessageRequest();
0 ignored issues
show
Bug introduced by
The property messageRequest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
	}
15
16
	/**
17
	 * @param array $labels
18
	 *
19
	 * @return Mail|string
20
	 * @throws \Exception
21
	 */
22 View Code Duplication
	public function addLabels( array $labels )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
	{
24
		$this->messageRequest->setAddLabelIds( $labels );
25
		try {
26
			return $this->modify();
27
		} catch ( \Exception $e ) {
28
			throw new \Exception( "Couldn't mark email as unread: {$e->getMessage()}" );
29
		}
30
	}
31
32
	/**
33
	 * @param array $labels
34
	 *
35
	 * @return Mail|string
36
	 * @throws \Exception
37
	 */
38 View Code Duplication
	public function removeLabels( array $labels )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
	{
40
		$this->messageRequest->setRemoveLabelIds( $labels );
41
		try {
42
			return $this->modify();
43
		} catch ( \Exception $e ) {
44
			throw new \Exception( "Couldn't remove mark email as important.: {$e->getMessage()}" );
45
		}
46
	}
47
48
	/**
49
	 * Adds a single label to the request
50
	 *
51
	 * @param $label
52
	 *
53
	 * @return Mail
54
	 */
55
	private function addSingleLabel( $label )
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
56
	{
57
		$this->messageRequest->setAddLabelIds( [ $label ] );
58
59
		return $this->modify();
60
	}
61
62
	/**
63
	 * Removes a single label from the request
64
	 *
65
	 * @param $label
66
	 *
67
	 * @return Mail
68
	 */
69
	private function removeSingleLabel( $label )
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
70
	{
71
		$this->messageRequest->setRemoveLabelIds( [ $label ] );
72
73
		return $this->modify();
74
	}
75
76
	/**
77
	 * Executes the modification
78
	 *
79
	 * @return Mail
80
	 */
81
	private function modify()
82
	{
83
		return new Mail( $this->service->users_messages->modify( 'me', $this->getId(), $this->messageRequest ) );
0 ignored issues
show
Bug introduced by
The property service does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
84
	}
85
}