1<?php
2/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
3
4namespace Icinga\Module\Setup;
5
6use ArrayIterator;
7use IteratorAggregate;
8use Icinga\Module\Setup\Exception\SetupException;
9
10/**
11 * Container for multiple configuration steps
12 */
13class Setup implements IteratorAggregate
14{
15    protected $steps;
16
17    protected $state;
18
19    public function __construct()
20    {
21        $this->steps = array();
22    }
23
24    public function getIterator()
25    {
26        return new ArrayIterator($this->getSteps());
27    }
28
29    public function addStep(Step $step)
30    {
31        $this->steps[] = $step;
32    }
33
34    public function addSteps(array $steps)
35    {
36        foreach ($steps as $step) {
37            $this->addStep($step);
38        }
39    }
40
41    public function getSteps()
42    {
43        return $this->steps;
44    }
45
46    /**
47     * Run the configuration and return whether it succeeded
48     *
49     * @return  bool
50     */
51    public function run()
52    {
53        $this->state = true;
54
55        try {
56            foreach ($this->steps as $step) {
57                $this->state &= $step->apply();
58            }
59        } catch (SetupException $_) {
60            $this->state = false;
61        }
62
63        return $this->state;
64    }
65
66    /**
67     * Return a summary of all actions designated to run
68     *
69     * @return  array       An array of HTML strings
70     */
71    public function getSummary()
72    {
73        $summaries = array();
74        foreach ($this->steps as $step) {
75            $summaries[] = $step->getSummary();
76        }
77
78        return $summaries;
79    }
80
81    /**
82     * Return a report of all actions that were run
83     *
84     * @return  array       An array of arrays of strings
85     */
86    public function getReport()
87    {
88        $reports = array();
89        foreach ($this->steps as $step) {
90            $report = $step->getReport();
91            if (! empty($report)) {
92                $reports[] = $report;
93            }
94        }
95
96        return $reports;
97    }
98}
99