Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.53% covered (success)
89.53%
77 / 86
78.57% covered (success)
78.57%
11 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
Http
89.53% covered (success)
89.53%
77 / 86
78.57% covered (success)
78.57%
11 / 14
41.83
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setSendStream
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setFetchStream
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSendStream
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFetchStream
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFetchedResult
77.78% covered (success)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
6.40
 hasFetchStream
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 send
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getStates
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getStateById
75.00% covered (success)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
4.25
 getStateByModel
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getStateByTimestamp
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 getStateByDate
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 getSnapshot
73.33% covered (success)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
7.93
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Audit\Adapter;
15
16use Pop\Http\Client\Stream;
17
18/**
19 * Auditor HTTP class
20 *
21 * @category   Pop
22 * @package    Pop\Audit
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    1.3.0
27 */
28class Http extends AbstractAdapter
29{
30
31    /**
32     * Stream to send the audit results
33     * @var Stream
34     */
35    protected $sendStream = null;
36
37    /**
38     * Stream to fetch the audit results
39     * @var Stream
40     */
41    protected $fetchStream = null;
42
43    /**
44     * Constructor
45     *
46     * Instantiate the HTTP adapter object
47     *
48     * @param Stream $sendStream
49     * @param Stream $fetchStream
50     */
51    public function __construct(Stream $sendStream, Stream $fetchStream = null)
52    {
53        $this->setSendStream($sendStream);
54        if (null !== $fetchStream) {
55            $this->setFetchStream($fetchStream);
56        }
57    }
58
59    /**
60     * Set the send stream
61     *
62     * @param  Stream $sendStream
63     * @return Http
64     */
65    public function setSendStream(Stream $sendStream)
66    {
67        $this->sendStream = $sendStream;
68        return $this;
69    }
70
71    /**
72     * Set the fetch stream
73     *
74     * @param  Stream $fetchStream
75     * @return Http
76     */
77    public function setFetchStream(Stream $fetchStream)
78    {
79        $this->fetchStream = $fetchStream;
80        return $this;
81    }
82
83    /**
84     * Get the send stream
85     *
86     * @return Stream
87     */
88    public function getSendStream()
89    {
90        return $this->sendStream;
91    }
92
93    /**
94     * Get the fetch stream
95     *
96     * @return Stream
97     */
98    public function getFetchStream()
99    {
100        return $this->fetchStream;
101    }
102
103    /**
104     * Get the fetched result
105     *
106     * @return mixed
107     */
108    public function getFetchedResult()
109    {
110        $resultResponse = null;
111
112        if (($this->fetchStream->hasResponse()) && ($this->fetchStream->getResponse()->hasBody())) {
113            $resultResponse = $this->fetchStream->getResponse()->getBody()->getContent();
114            if ($this->fetchStream->getResponse()->hasHeader('Content-Type')) {
115                if ($this->fetchStream->getResponse()->getHeader('Content-Type')->getValue() == 'application/json') {
116                    $resultResponse = json_decode($resultResponse, true);
117                } else if ($this->fetchStream->getResponse()->getHeader('Content-Type')->getValue() == 'application/x-www-form-urlencoded') {
118                    parse_str($resultResponse, $resultResponse);
119                }
120            }
121        }
122
123        return $resultResponse;
124    }
125
126    /**
127     * Determine if the adapter has a fetch stream
128     *
129     * @return boolean
130     */
131    public function hasFetchStream()
132    {
133        return (null !== $this->fetchStream);
134    }
135
136    /**
137     * Send the results of the audit
138     *
139     * @throws Exception
140     * @return Stream
141     */
142    public function send()
143    {
144        if (null === $this->action) {
145            throw new Exception('The model state differences have not been resolved.');
146        }
147        if ((null === $this->model) || (null === $this->modelId)) {
148            throw new Exception('The model has not been set.');
149        }
150
151        $this->sendStream->setFields($this->prepareData());
152        $this->sendStream->send();
153
154        return $this->sendStream;
155    }
156
157    /**
158     * Get model states
159     *
160     * @param  array $fields
161     * @return array
162     */
163    public function getStates(array $fields = [])
164    {
165        if (!empty($fields)) {
166            $this->fetchStream->setFields($fields);
167        }
168        $this->fetchStream->send();
169
170        return $this->getFetchedResult();
171    }
172
173    /**
174     * Get model state by ID
175     *
176     * @param  int     $id
177     * @param  boolean $asQuery
178     * @return array
179     */
180    public function getStateById($id, $asQuery = false)
181    {
182        $origUrl = $this->fetchStream->getUrl();
183
184        if ($asQuery) {
185            $this->fetchStream->setField('id', $id);
186        } else {
187            $this->fetchStream->setUrl($origUrl . '/' . $id);
188        }
189
190        $this->fetchStream->send();
191        $result = $this->getFetchedResult();
192
193        if (!empty($result['old'])) {
194            $result['old'] = json_decode($result['old'], true);
195        }
196        if (!empty($result['new'])) {
197            $result['new'] = json_decode($result['new'], true);
198        }
199
200        $this->fetchStream->setUrl($origUrl);
201
202        return $result;
203    }
204
205    /**
206     * Get model state by model
207     *
208     * @param  string $model
209     * @param  int    $modelId
210     * @return array
211     */
212    public function getStateByModel($model, $modelId = null)
213    {
214        $fields = [
215            'filter' => [
216                'model = ' . $model
217            ]
218        ];
219
220        if (null !== $modelId) {
221            $fields['filter'][] = 'model_id = ' . $modelId;
222        }
223
224        $this->fetchStream->setFields($fields);
225        $this->fetchStream->send();
226
227        return $this->getFetchedResult();
228    }
229
230    /**
231     * Get model state by timestamp
232     *
233     * @param  string $from
234     * @param  string $backTo
235     * @return array
236     */
237    public function getStateByTimestamp($from, $backTo = null)
238    {
239        $from = date('Y-m-d H:i:s', $from);
240
241        if (null !== $backTo) {
242            $backTo = date('Y-m-d H:i:s', $backTo);
243        }
244
245        $fields = [
246            'filter' => [
247                'timestamp <= ' . $from
248            ]
249        ];
250
251        if (null !== $backTo) {
252            $fields['filter'][] = 'timestamp >= ' . $backTo;
253        }
254
255        $this->fetchStream->setFields($fields);
256        $this->fetchStream->send();
257
258        return $this->getFetchedResult();
259    }
260
261    /**
262     * Get model state by date
263     *
264     * @param  string $from
265     * @param  string $backTo
266     * @return array
267     */
268    public function getStateByDate($from, $backTo = null)
269    {
270        if (strpos($from, ' ') === false) {
271            $from .= ' 23:59:59';
272        }
273
274        if (null !== $backTo) {
275            if (strpos($backTo, ' ') === false) {
276                $backTo .= ' 00:00:00';
277            }
278        }
279
280        $fields = [
281            'filter' => [
282                'timestamp <= ' . $from
283            ]
284        ];
285
286        if (null !== $backTo) {
287            $fields['filter'][] = 'timestamp >= ' . $backTo;
288        }
289
290        $this->fetchStream->setFields($fields);
291        $this->fetchStream->send();
292
293        return $this->getFetchedResult();
294    }
295
296    /**
297     * Get model snapshot by ID
298     *
299     * @param  int     $id
300     * @param  boolean $post
301     * @return array
302     */
303    public function getSnapshot($id, $post = false)
304    {
305        $url = $this->fetchStream->getUrl();
306        $this->fetchStream->setUrl($url . '/' . $id);
307        $this->fetchStream->send();
308
309        $result = $this->getFetchedResult();
310
311        if (!empty($result['old'])) {
312            $result['old'] = json_decode($result['old'], true);
313        }
314        if (!empty($result['new'])) {
315            $result['new'] = json_decode($result['new'], true);
316        }
317
318        $snapshot = [];
319
320        if (!($post) && !empty($result['old'])) {
321            $snapshot = $result['old'];
322        } else if (($post) && !empty($result['new'])) {
323            $snapshot = $result['new'];
324        }
325
326        $this->fetchStream->setUrl($url);
327
328        return $snapshot;
329    }
330
331}