NotificationManager::messages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Licensed under The GPL-3.0 License
4
 * For full copyright and license information, please see the LICENSE.txt
5
 * Redistributions of files must retain the above copyright notice.
6
 *
7
 * @since    2.0.0
8
 * @author   Christopher Castro <[email protected]>
9
 * @link     http://www.quickappscms.org
10
 * @license  http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
11
 */
12
namespace User\Notification;
13
14
use Cake\Utility\Inflector;
15
use User\Error\InvalidUserMessageException;
16
use User\Model\Entity\User;
17
use User\Notification\Message\BaseMessage;
18
19
/**
20
 * A simple class for handling user notification emails.
21
 *
22
 * ### Usage:
23
 *
24
 * ```php
25
 * $user = $this->Users->get($id);
26
 * $optionsArray = ['updateToken' => false];
27
 * $result = NotificationManager::welcome($user, $optionsArray)->send();
28
 * ```
29
 *
30
 * This class comes with a few built-in messages:
31
 *
32
 * - welcome
33
 * - activated
34
 * - blocked
35
 * - cancelRequest
36
 * - canceled
37
 * - passwordRequest
38
 *
39
 * ### Registering new messages:
40
 *
41
 * More messages can be registered (or overwritten) to this class using the
42
 * `addMessage()` method as follows:
43
 *
44
 * ```php
45
 * NotificationManager::addMessage('bye', 'ClassName\Extending\BaseMessage');
46
 * ```
47
 *
48
 * After registered you can start sending messages of `bye` type as below:
49
 *
50
 * ```php
51
 * NotificationManager::bye($user, $optionsArray)->send();
52
 * ```
53
 *
54
 * @method \User\Notification\Message\WelcomeMessage welcome(\Cake\Datasource\EntityInterface $user, array $config = [])
55
 * @method \User\Notification\Message\ActivatedMessage activated(\Cake\Datasource\EntityInterface $user, array $config = [])
56
 * @method \User\Notification\Message\BlockedMessage blocked(\Cake\Datasource\EntityInterface $user, array $config = [])
57
 * @method \User\Notification\Message\CancelRequestMessage cancelRequest(\Cake\Datasource\EntityInterface $user, array $config = [])
58
 * @method \User\Notification\Message\CanceledMessage canceled(\Cake\Datasource\EntityInterface $user, array $config = [])
59
 * @method \User\Notification\Message\PasswordRequestMessage passwordRequest(\Cake\Datasource\EntityInterface $user, array $config = [])
60
 */
61
class NotificationManager
62
{
63
64
    /**
65
     * Defaults registered messages.
66
     *
67
     * @var array
68
     */
69
    protected static $_messages = [
70
        'welcome' => 'User\\Notification\\Message\\WelcomeMessage',
71
        'activated' => 'User\\Notification\\Message\\ActivatedMessage',
72
        'blocked' => 'User\\Notification\Message\BlockedMessage',
73
        'cancelRequest' => 'User\\Notification\\Message\\CancelRequestMessage',
74
        'canceled' => 'User\\Notification\\Message\\CanceledMessage',
75
        'passwordRequest' => 'User\\Notification\\Message\\PasswordRequestMessage',
76
    ];
77
78
    /**
79
     * Magic method for dispatching to message handlers.
80
     *
81
     * @param string $method Name of the method
82
     * @param array $arguments Arguments for the invoked method
83
     * @return mixed
84
     */
85
    public static function __callStatic($method, $arguments)
86
    {
87
        if (!isset(static::$_messages[$method])) {
88
            throw new InvalidUserMessageException([$method, $method]);
89
        }
90
91
        if (empty($arguments[0]) ||
92
            !($arguments[0] instanceof User)
93
        ) {
94
            throw new \InvalidArgumentException(sprintf('The first argument for NotificationManager::%s() must be an User entity.', $name));
95
        }
96
97
        $handler = static::$_messages[$method];
98
        if (is_string($handler)) {
99
            if (!str_starts_with($handler, '\\')) {
100
                $handler = "\\{$handler}";
101
            }
102
103 View Code Duplication
            if (class_exists($handler)) {
104
                $config = !empty($arguments[1]) ? (array)$arguments[1] : [];
105
                $handler = new $handler($arguments[0], $config);
106
            }
107
        }
108
109
        if ($handler instanceof BaseMessage) {
110
            return $handler;
111
        }
112
113
        throw new InvalidUserMessageException([$method, $method]);
114
    }
115
116
    /**
117
     * Gets a list of all registered messages.
118
     *
119
     * @return array
120
     */
121
    public static function messages()
122
    {
123
        return static::$_messages;
124
    }
125
126
    /**
127
     * Looks for variables tags in the given message and replaces with their
128
     * corresponding values. For example, "[site:name] will be replaced with user's
129
     * real name.
130
     *
131
     * @param string $name CamelizedName for when using this message
132
     * @return string|\User\Notification\Message\BaseMessage Message handler. It can
133
     *  be either a string for a class name to instance, or a constructed message
134
     *  handler object.
135
     */
136
    public static function addMessage($name, $handler)
137
    {
138
        static::$_messages[Inflector::camelize($name)] = $handler;
139
    }
140
}
141