1+ <?php namespace DCarbone \PHPConsulAPI \Event ;
2+
3+ /*
4+ Copyright 2016 Daniel Carbone ([email protected] ) 5+
6+ Licensed under the Apache License, Version 2.0 (the "License");
7+ you may not use this file except in compliance with the License.
8+ You may obtain a copy of the License at
9+
10+ http://www.apache.org/licenses/LICENSE-2.0
11+
12+ Unless required by applicable law or agreed to in writing, software
13+ distributed under the License is distributed on an "AS IS" BASIS,
14+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ See the License for the specific language governing permissions and
16+ limitations under the License.
17+ */
18+
19+ use DCarbone \PHPConsulAPI \AbstractConsulClient ;
20+ use DCarbone \PHPConsulAPI \QueryOptions ;
21+ use DCarbone \PHPConsulAPI \Request ;
22+ use DCarbone \PHPConsulAPI \WriteOptions ;
23+
24+ /**
25+ * Class EventClient
26+ * @package DCarbone\PHPConsulAPI
27+ */
28+ class EventClient extends AbstractConsulClient
29+ {
30+ /**
31+ * @param UserEvent $event
32+ * @param WriteOptions|null $writeOptions
33+ * @return array(
34+ * @type UserEvent|null user event that was fired or null on error
35+ * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata
36+ * @type \DCarbone\PHPConsulAPI\Error error, if any
37+ * )
38+ */
39+ public function fire (UserEvent $ event , WriteOptions $ writeOptions = null )
40+ {
41+ $ r = new Request ('put ' , sprintf ('v1/event/fire/%s ' , rawurlencode ($ event ->getName ())), $ this ->_Config );
42+ $ r ->setWriteOptions ($ writeOptions );
43+
44+ if ('' !== ($ nf = $ event ->getNodeFilter ()))
45+ $ r ->params ()->set ('node ' , $ nf );
46+ if ('' !== ($ sf = $ event ->getServiceFilter ()))
47+ $ r ->params ()->set ('service ' , $ sf );
48+ if ('' !== ($ tf = $ event ->getTagFilter ()))
49+ $ r ->params ()->set ('tag ' , $ tf );
50+ if ('' !== ($ payload = $ event ->getPayload ()))
51+ $ r ->setBody ($ payload );
52+
53+ var_dump ($ payload );exit ;
54+
55+ list ($ duration , $ response , $ err ) = $ this ->requireOK ($ this ->doRequest ($ r ));
56+ $ wm = $ this ->buildWriteMeta ($ duration );
57+
58+ if (null !== $ err )
59+ return [null , $ wm , $ err ];
60+
61+ list ($ data , $ err ) = $ this ->decodeBody ($ response );
62+ if ($ err !== null )
63+ return [null , $ wm , $ err ];
64+
65+ return [new UserEvent ($ data ), $ wm , null ];
66+ }
67+
68+ /**
69+ * @param string $name
70+ * @param QueryOptions|null $queryOptions
71+ * @return array(
72+ * @type UserEvent[] list of user events or null on error
73+ * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata
74+ * @type \DCarbone\PHPConsulAPI\Error error, if any
75+ * )
76+ */
77+ public function eventList ($ name = '' , QueryOptions $ queryOptions = null )
78+ {
79+ $ r = new Request ('get ' , 'v1/event/list ' , $ this ->_Config );
80+ if ('' !== (string )$ name )
81+ $ r ->params ()->set ('name ' , $ name );
82+ $ r ->setQueryOptions ($ queryOptions );
83+
84+ list ($ duration , $ response , $ err ) = $ this ->requireOK ($ this ->doRequest ($ r ));
85+ $ qm = $ this ->buildQueryMeta ($ duration , $ response );
86+
87+ if (null !== $ err )
88+ return [null , $ qm , $ err ];
89+
90+ list ($ data , $ err ) = $ this ->decodeBody ($ response );
91+
92+ if (null !== $ err )
93+ return [null , $ qm , $ err ];
94+
95+ $ events = array ();
96+ foreach ($ data as $ event )
97+ {
98+ $ events [] = new UserEvent ($ event );
99+ }
100+
101+ return [$ events , $ qm , null ];
102+ }
103+ }
0 commit comments