summaryrefslogtreecommitdiff
path: root/src/Message/Handler.php
blob: 825cedabfcc9a676762946cffbe9ad527c0d560c (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace App\Message;

use App\Message\Overview;

/**
* Handles messages and context
*
* @author Phil Burton <phil@pgburton.com>
*/
class Handler
{
    /**
     * The important set of injects for this handler
     *
     * @var string[]
     */
    protected $important = [];

    /**
     * The important set of values of injects for this handler
     *
     * @var mixed[]
     */
    protected $importantValues = [];

    /**
     * Construct our handler
     *
     * @author Phil Burton <phil@pgburton.com>
     * @param string[] $important
     */
    public function __construct($important)
    {
        $this->setupImportant($important);
    }
    /**
     * Set the important list
     *
     * @author Phil Burton <phil@pgburton.com>
     * @param string[] $important
     */
    public function setupImportant($important = [])
    {
        $this->important = $important;
    }

    /**
     * Set important values
     *
     * @author Phil Burton <phil@pgburton.com>
     * @param mixed[] $array
     */
    protected function setImportantValues($array)
    {
        foreach ($array as $name => $value) {
            $this->addImportantValue($name, $value);
        }
    }

    /**
     * Add an important value to our array
     *
     * @author Phil Burton <phil@pgburton.com>
     * @param string $name
     * @param mixed[] $value
     */
    protected function addImportantValue($name, $value)
    {
        if (in_array($name, $this->important)) {
            $this->importantValues[$name] = $value;
        }

        return $name;
    }

    /**
     * Get a single important value
     *
     * @author Phil Burton <phil@pgburton.com>
     * @param string $name
     * @return mixed
     */
    public function returnImportant($name)
    {
        if (!array_key_exists($name, $this->importantValues)) {
            throw new \Exception('The var ' . $name . ' is not set in this context');
        }

        return $this->importantValues[$name];
    }

    /**
     * Magic __call override to be able to get improtant values using getMyImportant()
     * which then runs $this->returnImportant('myImportant')
     *
     * @author Phil Burton <phil@pgburton.com>
     * @param string $name
     * @param mixed[] $arguments
     * @return string
     */
    public function __call($name, $arguments)
    {
        if (strpos($name, 'get') === 0) {
            $this->returnImporant(substr($name, 3));
        }
    }

    /**
     * Render the opening message
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return string
     */
    public function renderOpeningMessage()
    {
        $overview = new Overview();

        $overview->setFromArray($this->importantValues);

        $message = $overview->renderMessage();

        $this->setImportantValues($overview->getImportantValues());

        return $message;
    }
}