Total Complexity | 8 |
Total Lines | 65 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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) |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 |