Passed
Push — hans/core-extraction ( ed2510...fc7dca )
by Simon
10:29 queued 08:21
created

CoreAdminTrait::coreCreate()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.1922

Importance

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