Passed
Pull Request — master (#91)
by
unknown
02:29
created

HasParts::getAllParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
        global $allParts;
20
        $allParts = collect([]);
21
        return $this->iterateParts($partsContainer);
22
    }
23
24
25
    /**
26
     * Recursive Method. Iterates through a collection,
27
     * finding all 'parts'.
28
     *
29
     * @param  collection  $partsContainer. F.e. collect([$message->payload])
30
     *
31
     * @return Collection of all 'parts' flattened
32
     * @return true if $retunfOnFirstFound is set to true and first part is found.
33
     */
34
35
    private function iterateParts( $partsContainer, $returnOnFirstFound = false)
36
    {
37
        global $allParts;
38
        $parts = $partsContainer->pluck('parts');
39
        if ($parts)
40
        {
41
            foreach ($parts as $part)
42
            {
43
                if (($part))
44
                {
45
                    if ($returnOnFirstFound){
46
                        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
47
                    }
48
49
                    $allParts[]=$part;
50
                    $part = collect($part);
51
                    $this->iterateParts($part);
52
                }
53
            }
54
55
        }
56
        return (collect($allParts))->flatten();
57
    }
58
}
59