initial commit
[namibia] / module / Utility / src / Utility / Comms / Ape.php
1 <?php
2 namespace Utility\Comms;
3
4
5 /**
6  * Facilitates chatting to APE http push server.
7  * @author andre.fourie
8  */
9 class Ape
10 {
11
12
13         const CHANGE_TYPE_REFRESH = 'Refresh';
14         const CHANGE_TYPE_CREATE  = 'Create';
15         const CHANGE_TYPE_UPDATE  = 'Update';
16         const CHANGE_TYPE_DELETE  = 'Delete';
17
18
19         /**
20          * Speak to the APE server on specified channel.
21          * @param  string $channel
22          * @param  array $data
23          * @param  string $raw
24          * @param  boolean|string $pubid
25          * @param  string $command
26          * @return array|boolean
27          */
28         static public function broadcast($channel, array $data, $raw = 'DATA', $pubid = false, $command = 'inlinepush')
29         {
30                 $gearClient = new \GearmanClient();
31                 $gearClient->addServer();
32                 $gearClient->doBackground(
33                         'Broadcast',
34                         json_encode($data)
35                 );
36
37                 /*$cmd = \Zend\Json\Json::encode(array(array(
38                                 'cmd' => $command,
39                                 'params' =>  array(
40                                                 'password'  => 'ap3dP2ssk3Y',
41                                                 'pubid'     => $pubid,
42                                                 'raw'       => $raw,
43                                                 'channel'   => $channel,
44                                                 'data'      => $data
45                                 )
46                 )));
47                 try
48                 {
49                         if (false && function_exists('curl_init'))
50                         {
51                                 #-> Have curl, use it.
52                                 $ch = curl_init('http://127.0.0.1:6969');
53                                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
54                                 curl_setopt($ch, CURLOPT_POSTFIELDS, $cmd);
55                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
56                                 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
57                                                 'Content-Type: application/json',
58                                                 'Content-Length: ' . strlen($cmd))
59                                 );
60                                 $result = curl_exec($ch);
61                                 curl_close($ch);
62                                 return \Zend\Json\Json::decode($result);
63                         }
64                         else
65                         {
66                                 #-> No curl, use the slower option.
67                                 $result = @file_get_contents('http://127.0.0.1:6969', null, stream_context_create(array(
68                                         'http' => array(
69                                                 'method'  => 'POST',
70                                                 'header'  => 'Content-Type: application/json' . "\r\n"
71                                                                    . 'Content-Length: ' . strlen($cmd) . "\r\n",
72                                                 'content' => $cmd
73                                         )
74                                 )));
75                                 return \Zend\Json\Json::decode($result);
76                         }
77                 }
78                 catch (\Exception $e)
79                 {
80                         error_log(__CLASS__ . " >> $e");
81                         return false;
82                 }*/
83         }
84
85         /**
86          * Broadcast a Dataset Refresh notification.
87          * @param string $datasetName
88          * @return array|boolean
89          */
90         static public function broadcastBuildDatasetChange($datasetName)
91         {
92                 return self::broadcast('synchv1', array(
93                                 'Meta' => array(
94                                         'Strategy'    => 'Build',
95                                         'DatasetName' => $datasetName,
96                                         'Type'        => self::CHANGE_TYPE_REFRESH
97                                 ),
98                                 'Data' => array()
99                 ));
100         }
101
102         /**
103          * Broadcast a Dataset Update notification.
104          * @param string $datasetName
105          * @param string $changeType
106          * @param array  $datasetRecord
107          * @return array|boolean
108          */
109         static public function broadcastUpdateDatasetChange($datasetName, $changeType, array $datasetRecord)
110         {
111                 return self::broadcast('synchv1', array(
112                                 'Meta' => array(
113                                         'Strategy'    => 'Update',
114                                         'DatasetName' => $datasetName,
115                                         'Type'        => $changeType
116                                 ),
117                                 'Data' => $datasetRecord
118                 ));
119         }
120
121
122 }
123