File size: 2,792 Bytes
6966756
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
<?php

namespace App\Notify;

class Notify
{
    /*
    |--------------------------------------------------------------------------
    | Send Notification
    |--------------------------------------------------------------------------
    |
    | The notification will go via some methods which were implemented. Different
    | classes are available for a particular method. But we need a central position
    | to serve a notification via every method which is selected. This is
    | the class that is serving this perspective.
    |
    */


    /**
    * Template name, which contain the short codes and messages
    *
    * @var string
    */
	public $templateName;

    /**
    * Short Codes, which will be replaced
    *
    * @var array
    */
	public $shortCodes;

    /**
    * Send via email,sms etc
    *
    * @var array|null
    */
	public $sendVia;

    /**
    * Instance of user, who will get the notification
    *
    * @var object
    */
	public $user;

    /**
    * Notification log will be created or not
    *
    * @var bool
    */
	public $createLog;

    /**
    * System general setting's instances
    *
    * @var object
    */
	public $setting;

    /**
    * The relational field name like user_id, agent_id
    *
    * @var string
    */
	public $userColumn;

    /**
    * Image for push notification
    *
    * @var string|null
    */
	public $pushImage;

    /**
    * Assign value to sendVia and setting property
    *
    * @param null $sendVia
    * @return void
    */
	public function __construct($sendVia = null)
	{
		$this->sendVia = $sendVia;
	}

    /**
    * Send notification via methods.
    *
    * This method is creating instances of notifications to send the notification.
    *
    * @return void
    */
	public function send(){
		$methods = [];

        //get the notification method classes which are selected
		if($this->sendVia){
			foreach ($this->sendVia as $sendVia) {
				$methods[$sendVia] = $this->notifyMethods($sendVia);
			}
		}else{
			$methods = $this->notifyMethods();
		}

        //send the notification via methods one by one
		foreach($methods as $method){
			$notify = new $method;
			$notify->templateName = $this->templateName;
			$notify->shortCodes = $this->shortCodes;
			$notify->user = $this->user;
			$notify->createLog = $this->createLog;
			$notify->userColumn = $this->userColumn;
			$notify->pushImage = $this->pushImage;
			$notify->send();
		}
	}

    /**
    * Get the notification method classes.
    *
    * @param array|null $sendVia
    * @return array|string
    */
	protected function notifyMethods($sendVia = null){
		$methods = [
			'email'=>Email::class,
			'sms'=>Sms::class,
            'push'=>Push::class,
		];
		if ($sendVia) {
			return $methods[$sendVia];
		}
		return $methods;
	}
}