Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Post
92.31% covered (success)
92.31%
12 / 13
75.00% covered (warning)
75.00%
3 / 4
5.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setContentType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBody
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 doWrite
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
1<?php
2
3/**
4 * HTTP POST log writer
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Error_Logging
25 * @author   Chris Hallberg <challber@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Log\Writer;
31
32use Laminas\Http\Client;
33
34use function is_array;
35
36/**
37 * This class extends the Laminas Logging to sent POST messages over HTTP
38 *
39 * @category VuFind
40 * @package  Error_Logging
41 * @author   Chris Hallberg <challber@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org Main Site
44 */
45class Post extends \Laminas\Log\Writer\AbstractWriter
46{
47    use VerbosityTrait;
48
49    /**
50     * Holds the verbosity level
51     *
52     * @var int
53     */
54    protected $url = null;
55
56    /**
57     * Pre-configured http client
58     *
59     * @var \Laminas\Http\Client
60     */
61    protected $client = null;
62
63    /**
64     * Content type
65     *
66     * @var string
67     */
68    protected $contentType = 'application/x-www-form-urlencoded';
69
70    /**
71     * Constructor
72     *
73     * @param string $url    URL to open as a stream
74     * @param Client $client Pre-configured http client
75     */
76    public function __construct($url, Client $client)
77    {
78        $this->url = $url;
79        $this->client = $client;
80    }
81
82    /**
83     * Set verbosity
84     *
85     * @param int $type content type string
86     *
87     * @return void
88     */
89    public function setContentType($type)
90    {
91        $this->contentType = $type;
92    }
93
94    /**
95     * Get data for raw body
96     *
97     * @param array $event event data
98     *
99     * @return string
100     */
101    protected function getBody($event)
102    {
103        return json_encode(
104            ['message' => $this->formatter->format($event) . PHP_EOL]
105        );
106    }
107
108    /**
109     * Write a message to the log.
110     *
111     * @param array $event event data
112     *
113     * @return void
114     * @throws \Laminas\Log\Exception\RuntimeException
115     */
116    protected function doWrite(array $event)
117    {
118        // Apply verbosity filter:
119        if (is_array($event['message'])) {
120            $event['message'] = $event['message'][$this->verbosity];
121        }
122
123        // Create request
124        $this->client->setUri($this->url);
125        $this->client->setMethod('POST');
126        $this->client->setEncType($this->contentType);
127        $this->client->setRawBody($this->getBody($this->applyVerbosity($event)));
128        // Send
129        $this->client->send();
130    }
131}