Passed
Push — feature/Logging ( b46de4...c811a9 )
by Simon
07:07
created

SolrLogger::logMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.9332
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Helpers;
5
6
use Firesphere\SolrSearch\Models\SolrLog;
7
use Firesphere\SolrSearch\Services\SolrCoreService;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Exception\GuzzleException;
10
use SilverStripe\Dev\Debug;
11
use SilverStripe\ORM\ValidationException;
12
13
class SolrLogger
14
{
15
    /**
16
     * @var Client
17
     */
18
    protected $client;
19
20
    /**
21
     * SolrLogger constructor.
22 29
     * @param null $handler
1 ignored issue
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $handler is correct as it would always require null to be passed?
Loading history...
23
     */
24 29
    public function __construct($handler = null)
25 29
    {
26 29
        $config = SolrCoreService::config()->get('config');
27
        $endpoint = $config['endpoint'];
28 29
        $hostConfig = array_values($endpoint);
29
        $guzzleConfig = [
30 29
            'base_uri' => $hostConfig[0]['host'] . ':' . $hostConfig[0]['port']
31 1
        ];
32
        if ($handler) {
1 ignored issue
show
introduced by
$handler is of type null, thus it always evaluated to false.
Loading history...
33
            $guzzleConfig['handler'] = $handler;
34 29
        }
35 29
36
        $this->client = new Client($guzzleConfig);
37
    }
38
39
    /**
40
     * @param string $type
41
     * @param string $message
42 27
     * @param string $index
43
     * @throws GuzzleException
44 27
     * @throws ValidationException
45 27
     */
46
    public static function logMessage($type, $message, $index)
47
    {
48
        $solrLogger = new self();
49
        $solrLogger->saveSolrLog('Index');
50
        /** @var SolrLog $lastError */
51 27
        $lastError = SolrLog::get()
52
            ->filter([
53 27
                'Index' => 'x:' . $index,
54
                'Level' => $type
55 27
            ])
56 27
            ->sort('Created ASC')
57 27
            ->first();
58
59 27
        $err = ($lastError === null) ? 'Unknown' : $lastError->getLastErrorLine();
60 27
        Debug::dump($message . "\n" . $err);
61 27
    }
62 27
63 27
    /**
64 27
     * @param string $type
65 27
     * @throws GuzzleException
66 27
     * @throws ValidationException
67
     */
68
    public function saveSolrLog($type = 'Query'): void
69 27
    {
70
        $response = $this->client->request('GET', 'solr/admin/info/logging', [
71
            'query' => [
72
                'since' => 0,
73
                'wt'    => 'json'
74 2
            ]
75
        ]);
76 2
77
        $arrayResponse = json_decode($response->getBody(), true);
78
79
        foreach ($arrayResponse['history']['docs'] as $error) {
80
            $filter = [
81
                'Timestamp' => $error['time'],
82
                'Level'     => $error['level'],
83 1
                'Index'     => $error['core'],
84
            ];
85 1
            if (!SolrLog::get()->filter($filter)->exists()) {
86
                SolrLog::create([
87 1
                    'Timestamp' => $error['time'],
88
                    'Message'   => $error['message'],
89
                    'Index'     => $error['core'],
90
                    'Type'      => $type,
91
                    'Level'     => $error['level'],
92
                ])->write();
93
            }
94
        }
95
    }
96
97
    /**
98
     * @return Client
99
     */
100
    public function getClient(): Client
101
    {
102
        return $this->client;
103
    }
104
105
    /**
106
     * @param Client $client
107
     * @return SolrLogger
108
     */
109
    public function setClient(Client $client): self
110
    {
111
        $this->client = $client;
112
113
        return $this;
114
    }
115
}
116