Completed
Push — master ( 058435...d3162a )
by Patrick
02:34
created

Queueable::build()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.2
cc 4
eloc 9
nc 6
nop 0
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Builders\Traits;
4
5
use LaravelDoctrine\Fluent\Buildable;
6
use LaravelDoctrine\Fluent\Builders\Delay;
7
8
trait Queueable
9
{
10
    /**
11
     * @var Buildable[]
12
     */
13
    protected $queued = [];
14
15
    /**
16
     * @param Buildable $buildable
17
     */
18
    public function queue(Buildable $buildable)
19
    {
20
        $this->queued[] = $buildable;
21
    }
22
23
    /**
24
     * @param Buildable     $buildable
25
     * @param callable|null $callback
26
     */
27
    public function callbackAndQueue(Buildable $buildable, callable $callback = null)
28
    {
29
        if (is_callable($callback)) {
30
            $callback($buildable);
31
        }
32
33
        $this->queue($buildable);
34
    }
35
36
    /**
37
     * Execute the build process for all queued buildables
38
     */
39
    public function build()
40
    {
41
        $delayed = [];
42
        foreach ($this->getQueued() as $buildable) {
43
            if ($buildable instanceof Delay) {
44
                $delayed[] = $buildable;
45
            } else {
46
                $buildable->build();
47
            }
48
        }
49
50
        // We will delay some of the builds, because they
51
        // depend on the executing of the other builds
52
        foreach ($delayed as $buildable) {
53
            $buildable->build();
54
        }
55
    }
56
57
    /**
58
     * @return \LaravelDoctrine\Fluent\Buildable[]
59
     */
60
    public function getQueued()
61
    {
62
        return $this->queued;
63
    }
64
}
65