summaryrefslogtreecommitdiffstats
path: root/modules/setup/library/Setup/Setup.php
blob: 7b0baed365398d7cb195a3003172a73eb1b40fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup;

use ArrayIterator;
use IteratorAggregate;
use Icinga\Module\Setup\Exception\SetupException;
use Traversable;

/**
 * Container for multiple configuration steps
 */
class Setup implements IteratorAggregate
{
    protected $steps;

    protected $state;

    public function __construct()
    {
        $this->steps = array();
    }

    public function getIterator(): Traversable
    {
        return new ArrayIterator($this->getSteps());
    }

    public function addStep(Step $step)
    {
        $this->steps[] = $step;
    }

    public function addSteps(array $steps)
    {
        foreach ($steps as $step) {
            $this->addStep($step);
        }
    }

    public function getSteps()
    {
        return $this->steps;
    }

    /**
     * Run the configuration and return whether it succeeded
     *
     * @return  bool
     */
    public function run()
    {
        $this->state = true;

        try {
            foreach ($this->steps as $step) {
                $this->state &= $step->apply();
            }
        } catch (SetupException $_) {
            $this->state = false;
        }

        return $this->state;
    }

    /**
     * Return a summary of all actions designated to run
     *
     * @return  array       An array of HTML strings
     */
    public function getSummary()
    {
        $summaries = array();
        foreach ($this->steps as $step) {
            $summaries[] = $step->getSummary();
        }

        return $summaries;
    }

    /**
     * Return a report of all actions that were run
     *
     * @return  array       An array of arrays of strings
     */
    public function getReport()
    {
        $reports = array();
        foreach ($this->steps as $step) {
            $report = $step->getReport();
            if (! empty($report)) {
                $reports[] = $report;
            }
        }

        return $reports;
    }
}