Passed
Push — master ( 7acea0...11875a )
by Simon
06:53
created

CoreAdminTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 29
c 1
b 0
f 0
dl 0
loc 89
ccs 25
cts 29
cp 0.8621
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A coreReload() 0 10 1
A coreCreate() 0 16 2
A coreUnload() 0 9 1
A coreStatus() 0 9 1
1
<?php
2
/**
3
 * Trait CoreAdminTrait|Firesphere\SolrSearch\Traits\CoreAdminTrait is the trait that helps with Admin operations.
4
 * Core operations, such as reload, load, create, unload etc. are supplied by this trait.
5
 *
6
 * @package Firesphere\SolrSearch\Traits
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
12
namespace Firesphere\SolrSearch\Traits;
13
14
use Exception;
15
use Firesphere\SolrSearch\Helpers\SolrLogger;
16
use Firesphere\SolrSearch\Interfaces\ConfigStore;
17
use GuzzleHttp\Exception\GuzzleException;
18
use Solarium\Client;
19
use Solarium\QueryType\Server\CoreAdmin\Query\Query;
20
use Solarium\QueryType\Server\CoreAdmin\Result\StatusResult;
21
22
/**
23
 * Trait CoreAdminTrait is the trait that helps with Admin operations.
24
 * Core operations, such as reload, load, create, unload etc. are supplied by this trait.
25
 *
26
 * @package Firesphere\SolrSearch\Traits
27
 */
28
trait CoreAdminTrait
29
{
30
    /**
31
     * @var Query A core admin object
32
     */
33
    protected $admin;
34
    /**
35
     * @var Client The current client
36
     */
37
    protected $client;
38
39
    /**
40
     * Create a new core
41
     *
42
     * @param $core string - The name of the core
43
     * @param ConfigStore $configStore
44
     * @return bool
45
     * @throws Exception
46
     * @throws GuzzleException
47
     */
48 2
    public function coreCreate($core, $configStore): bool
49
    {
50 2
        $action = $this->admin->createCreate();
51
52 2
        $action->setCore($core);
53 2
        $action->setInstanceDir($configStore->instanceDir($core));
54 2
        $this->admin->setAction($action);
55
        try {
56 2
            $response = $this->client->coreAdmin($this->admin);
57
58 2
            return $response->getWasSuccessful();
59
        } catch (Exception $e) {
60
            $solrLogger = new SolrLogger();
61
            $solrLogger->saveSolrLog('Config');
62
63
            throw new Exception($e);
64
        }
65
    }
66
67
    /**
68
     * Reload the given core
69
     *
70
     * @param $core
71
     * @return StatusResult|null
72
     */
73 35
    public function coreReload($core)
74
    {
75 35
        $reload = $this->admin->createReload();
76 35
        $reload->setCore($core);
77
78 35
        $this->admin->setAction($reload);
79
80 35
        $response = $this->client->coreAdmin($this->admin);
81
82 35
        return $response->getStatusResult();
83
    }
84
85
    /**
86
     * Get the core status
87
     *
88
     * @param string $core
89
     * @return StatusResult|null
90
     */
91 35
    public function coreStatus($core)
92
    {
93 35
        $status = $this->admin->createStatus();
94 35
        $status->setCore($core);
95
96 35
        $this->admin->setAction($status);
97 35
        $response = $this->client->coreAdmin($this->admin);
98
99 35
        return $response->getStatusResult();
100
    }
101
102
    /**
103
     * Remove a core from Solr
104
     *
105
     * @param string $core core name
106
     * @return StatusResult|null A result is successful
107
     */
108 1
    public function coreUnload($core)
109
    {
110 1
        $unload = $this->admin->createUnload();
111 1
        $unload->setCore($core);
112
113 1
        $this->admin->setAction($unload);
114 1
        $response = $this->client->coreAdmin($this->admin);
115
116 1
        return $response->getStatusResult();
117
    }
118
}
119