Queueable::build()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

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