summaryrefslogtreecommitdiff
path: root/src/Message/Base.php
blob: 4565de91082619b4bd03674a48def0dd0be97926 (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
<?php

namespace App\Message;

use Mustache_Engine;
use App\Config\Config;
use App\Message\Handler;

/**
* Base class for a message
*/
class Base
{
    protected $message_name;
    protected $config;
    protected $message;
    protected $injects = [];
    protected $important;

    protected function loadMessage()
    {
        if (!array_key_exists($this->message_name, $this->config)) {
            throw new Exception('Could not load messge from config (' . $this->message_name . ')');
        }

        $this->message = $this->config[$this->message_name];
    }

    protected function loadConfig()
    {
        $config =Config::getConfig();

        if (!$config) {
            throw new \Exception('Could not load config');
        }

        $this->config = $config;
    }

    public function __construct()
    {
        $this->loadConfig();
            
        $this->loadMessage();
    }

    public function renderMessage()
    {
        $m = new Mustache_Engine;
        return $m->render($this->message, $this->getInjectValues());
    }

    public function getInjectValues()
    {
        $out = [];
            foreach ($this->injects as $inject) {
                $out[$inject] = $this->$inject;
            }

        return $out;
    }

    public function getImportantValues()
    {
            $out = [];
            foreach ($this->important as $important) {
                $out[$important] = $this->$important;
            }

        return $out;
    }

    public function setFromArray(array $array)
    {
        foreach ($array as $key => $value) {
            if (in_array($key, $this->injects)) {
                $this->$key = (string) $value;
            }
        }
    }
}