Passed
Pull Request — master (#225)
by Simon
21:24 queued 05:14
created

SolrLogger::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * class SolrLogger|Firesphere\SolrSearch\Helpers\SolrLogger Log errors to the Database
4
 *
5
 * @package Firesphere\SolrSearch\Helpers
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SolrSearch\Helpers;
11
12
use Countable;
13
use Firesphere\SolrSearch\Models\SolrLog;
14
use Firesphere\SolrSearch\Services\SolrCoreService;
15
use GuzzleHttp\Client;
16
use GuzzleHttp\Exception\GuzzleException;
17
use Psr\Log\LoggerInterface;
18
use SilverStripe\Control\Controller;
19
use SilverStripe\Control\Director;
20
use SilverStripe\Core\Injector\Injector;
21
use SilverStripe\Dev\Debug;
22
use SilverStripe\ORM\DB;
23
use SilverStripe\ORM\ValidationException;
24
25
/**
26
 * Class SolrLogger
27
 *
28
 * Log information from Solr to the CMS for reference
29
 *
30
 * @package Firesphere\SolrSearch\Helpers
31
 */
32
class SolrLogger
33
{
34
    /**
35
     * @var Client Guzzle base client to communicate with Solr
36
     */
37
    protected $client;
38
39
    /**
40
     * @var array
41
     */
42
    protected $options = [];
43
44
    /**
45
     * SolrLogger constructor.
46
     *
47
     * @param null|Countable $handler
48
     */
49 43
    public function __construct($handler = null)
50
    {
51 43
        $config = SolrCoreService::config()->get('config');
52 43
        $hostConfig = array_shift($config['endpoint']);
53
        $guzzleConfig = [
54 43
            'base_uri' => $hostConfig['host'] . ':' . $hostConfig['port'],
55
        ];
56 43
        if ($handler) {
57 1
            $guzzleConfig['handler'] = $handler;
58
        }
59
60 43
        if (isset($hostConfig['username']) && isset($hostConfig['password'])) {
61 43
            $this->options = [
62
                'auth' => [
63 43
                    $hostConfig['username'],
64 43
                    $hostConfig['password']
65
                ]
66
            ];
67
        }
68
69
70 43
        $this->client = new Client($guzzleConfig);
71 43
    }
72
73
    /**
74
     * Log the given message and dump it out.
75
     * Also boot the Log to get the latest errors from Solr
76
     *
77
     * @param string $type
78
     * @param string $message
79
     * @throws GuzzleException
80
     * @throws ValidationException
81
     */
82 1
    public static function logMessage($type, $message): void
83
    {
84 1
        $solrLogger = new self();
85 1
        $solrLogger->saveSolrLog($type);
86
        /** @var SolrLog $lastError */
87 1
        $lastError = SolrLog::get()->last();
88
89 1
        $err = ($lastError === null) ? 'Unknown' : $lastError->getLastErrorLine();
90 1
        $message .= 'Last known error:' . PHP_EOL . $err;
91
        /** @var LoggerInterface $logger */
92 1
        $logger = Injector::inst()->get(LoggerInterface::class);
93 1
        $logger->alert($message);
94 1
        if (Director::is_cli() || Controller::curr()->getRequest()->getVar('unittest')) {
95 1
            Debug::dump($message);
96
        }
97 1
    }
98
99
    /**
100
     * Save the latest Solr errors to the log
101
     *
102
     * @param string $type
103
     * @throws GuzzleException
104
     * @throws ValidationException
105
     */
106 40
    public function saveSolrLog($type = 'Query'): void
107
    {
108 40
        $options = array_merge($this->options, [
109 40
            'query' => [
110
                'since' => 0,
111
                'wt'    => 'json',
112
            ],
113
        ]);
114 40
        $response = $this->client->get('solr/admin/info/logging', $options);
115
116 40
        $arrayResponse = json_decode($response->getBody(), true);
117
118 40
        foreach ($arrayResponse['history']['docs'] as $error) {
119
            $filter = [
120 1
                'Timestamp' => $error['time'],
121 1
                'Index'     => $error['core'] ?? 'x:Unknown',
122 1
                'Level'     => $error['level'],
123
            ];
124 1
            $this->findOrCreateLog($type, $filter, $error);
125
        }
126 40
    }
127
128
    /**
129
     * Attempt to find, otherwise create, a log object
130
     *
131
     * @param $type
132
     * @param array $filter
133
     * @param $error
134
     * @throws ValidationException
135
     */
136 1
    private function findOrCreateLog($type, array $filter, $error): void
137
    {
138
        // Not covered in tests. It's only here to make sure the connection isn't closed by a child process
139 1
        $conn = DB::is_active();
140
        // @codeCoverageIgnoreStart
141
        if (!$conn) {
142
            $config = DB::getConfig();
143
            DB::connect($config);
144
        }
145
        // @codeCoverageIgnoreEnd
146 1
        if (!SolrLog::get()->filter($filter)->exists()) {
147
            $logData = [
148 1
                'Message' => $error['message'],
149 1
                'Type'    => $type,
150
            ];
151 1
            $log = array_merge($filter, $logData);
152 1
            SolrLog::create($log)->write();
153 1
            if (Director::is_cli() || Controller::curr()->getRequest()->getVar('unittest')) {
154
                /** @var LoggerInterface $logger */
155 1
                $logger = Injector::inst()->get(LoggerInterface::class);
156 1
                $logger->error($error['message']);
157
            }
158
        }
159 1
    }
160
161
    /**
162
     * Return the Guzzle Client
163
     *
164
     * @return Client
165
     */
166 2
    public function getClient(): Client
167
    {
168 2
        return $this->client;
169
    }
170
171
    /**
172
     * Set the Guzzle client
173
     *
174
     * @param Client $client
175
     * @return SolrLogger
176
     */
177 1
    public function setClient(Client $client): self
178
    {
179 1
        $this->client = $client;
180
181 1
        return $this;
182
    }
183
184
    /**
185
     * Get the options for Guzzle
186
     *
187
     * @return array
188
     */
189 1
    public function getOptions(): array
190
    {
191 1
        return $this->options;
192
    }
193
194
    /**
195
     * Set custom options for Guzzle
196
     *
197
     * @param array $options
198
     */
199 1
    public function setOptions(array $options): void
200
    {
201 1
        $this->options = $options;
202 1
    }
203
}
204