PackageBuilder::install()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
ccs 7
cts 7
cp 1
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the skeleton package.
7
 *
8
 * (c) Gennady Knyazkin <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Gennadyx\Skeleton;
15
16
use Composer\Script\Event;
17
use Gennadyx\Skeleton\Action\ActionInterface;
18
use MF\Collection\Immutable\Generic\Map;
19
20
/**
21
 * Class PackageBuilder
22
 *
23
 * @author Gennady Knyazkin <[email protected]>
24
 */
25
class PackageBuilder
26
{
27
    /**
28
     * Execute all actions
29
     *
30
     * @param Event $event
31
     *
32
     * @return void
33
     * @throws Exception\RuntimeException
34
     */
35 2
    public static function install(Event $event)
36
    {
37 2
        $packageBuilder = new static();
38 2
        $vars           = $packageBuilder->collectVars();
39 2
        $callback       = $packageBuilder->getEachCallback($vars, $event);
40
41
        /** @var ActionQueue $actions */
42 2
        $actions = $packageBuilder->createActionQueue();
43 2
        $actions->each($callback);
44 2
    }
45
46
    /**
47
     * @param Map   $vars
48
     * @param Event $event
49
     *
50
     * @return \Closure
51
     */
52
    protected function getEachCallback(Map $vars, Event $event): \Closure
53
    {
54 2
        return function (ActionInterface $action) use ($vars, $event) {
55 2
            if ($action instanceof VarAwareInterface) {
56 2
                $action->setVars($vars);
57
            }
58
59 2
            if ($action instanceof EventAwareInterface) {
60 2
                $action->setEvent($event);
61
            }
62
63 2
            $action->execute();
64 2
        };
65
    }
66
67
    /**
68
     * Create action queue
69
     *
70
     * @return ActionQueue|ActionInterface[]
71
     */
72 2
    protected function createActionQueue(): ActionQueue
73
    {
74 2
        return ActionQueue::create();
75
    }
76
77
    /**
78
     * Collect vars
79
     *
80
     * @return Map
81
     */
82 2
    protected function collectVars(): Map
83
    {
84 2
        return VarLoader::collect();
85
    }
86
}
87