Passed
Push — master ( d8020b...a396f6 )
by Daniel
05:56 queued 03:38
created

HasParts   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B iterateParts() 0 24 7
A getAllParts() 0 5 1
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
use Google_Service_Gmail_MessagePart;
6
use Illuminate\Support\Collection;
7
8
trait HasParts
9
{
10
	/**
11
	 * LOL
12
	 * @var Collection
13
	 */
14
	private $allParts;
15
16
	/**
17
	 * Find all Parts of a message.
18
	 * Necessary to reset the $allParts Varibale.
19
	 *
20
	 * @param  collection  $partsContainer  . F.e. collect([$message->payload])
21
	 *
22
	 * @return Collection of all 'parts' flattened
23
	 */
24
	private function getAllParts($partsContainer)
25
	{
26
		$this->iterateParts($partsContainer);
27
28
		return collect($this->allParts);
29
	}
30
31
32
	/**
33
	 * Recursive Method. Iterates through a collection,
34
	 * finding all 'parts'.
35
	 *
36
	 * @param  collection  $partsContainer
37
	 * @param  bool  $returnOnFirstFound
38
	 *
39
	 * @return Collection|boolean
40
	 */
41
42
	private function iterateParts($partsContainer, $returnOnFirstFound = false)
43
	{
44
		$parts = [];
45
46
		$plucked = $partsContainer->flatten()->filter();
47
48
		if ($plucked->count()) {
49
			$parts = $plucked;
50
		} else {
51
			if ($partsContainer->count()) {
52
				$parts = $partsContainer;
53
			}
54
		}
55
56
		if ($parts) {
57
			/** @var Google_Service_Gmail_MessagePart $part */
58
			foreach ($parts as $part) {
59
				if ($part) {
60
					if ($returnOnFirstFound) {
61
						return true;
62
					}
63
64
					$this->allParts[$part->getPartId()] = $part;
65
					$this->iterateParts(collect($part->getParts()), false);
66
				}
67
			}
68
		}
69
	}
70
}
71