Passed
Pull Request — master (#87)
by Daniel
02:59
created

HasParts   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 47
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A iterateParts() 0 21 5
A getAllParts() 0 3 1
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
use Illuminate\Support\Collection;
6
7
trait HasParts
8
{
9
	/**
10
	 * Find all Parts of a message.
11
	 * Necessary to reset the $allParts Varibale.
12
	 *
13
	 * @param  collection  $partsContainer  . F.e. collect([$message->payload])
14
	 *
15
	 * @return Collection of all 'parts' flattened
16
	 */
17
	private function getAllParts($partsContainer)
18
	{
19
		return $this->iterateParts($partsContainer);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->iterateParts($partsContainer) also could return the type true which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
20
	}
21
22
23
	/**
24
	 * Recursive Method. Iterates through a collection,
25
	 * finding all 'parts'.
26
	 *
27
	 * @param  collection  $partsContainer
28
	 * @param  bool  $returnOnFirstFound
29
	 *
30
	 * @return Collection|boolean
31
	 */
32
33
	private function iterateParts($partsContainer, $returnOnFirstFound = false)
34
	{
35
		$allParts = [];
36
		$parts = $partsContainer->pluck('parts');
37
38
		if ($parts) {
0 ignored issues
show
introduced by
$parts is of type Illuminate\Support\Collection, thus it always evaluated to true.
Loading history...
39
			foreach ($parts as $part) {
40
				if ($part) {
41
					if ($returnOnFirstFound) {
42
						return true;
43
					}
44
45
					$allParts[] = $part;
46
					$part = collect($part);
47
					$this->iterateParts($part);
48
				}
49
			}
50
51
		}
52
53
		return (collect($allParts))->flatten();
54
	}
55
}
56