Passed
Pull Request — master (#216)
by Simon
08:31 queued 06:08
created

CoreAdminTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A coreUnload() 0 9 1
A coreReload() 0 10 1
A coreStatus() 0 9 1
A coreCreate() 0 17 2
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
            // @codeCoverageIgnoreStart
60
        } catch (Exception $error) {
61
            $solrLogger = new SolrLogger();
62
            $solrLogger->saveSolrLog('Config');
63
64
            throw new Exception($error);
65
        }
66
        // @codeCoverageIgnoreEnd
67
    }
68
69
    /**
70
     * Reload the given core
71
     *
72
     * @param $core
73
     * @return StatusResult|null
74
     */
75 35
    public function coreReload($core)
76
    {
77 35
        $reload = $this->admin->createReload();
78 35
        $reload->setCore($core);
79
80 35
        $this->admin->setAction($reload);
81
82 35
        $response = $this->client->coreAdmin($this->admin);
83
84 35
        return $response->getStatusResult();
85
    }
86
87
    /**
88
     * Get the core status
89
     *
90
     * @param string $core
91
     * @return StatusResult|null
92
     */
93 35
    public function coreStatus($core)
94
    {
95 35
        $status = $this->admin->createStatus();
96 35
        $status->setCore($core);
97
98 35
        $this->admin->setAction($status);
99 35
        $response = $this->client->coreAdmin($this->admin);
100
101 35
        return $response->getStatusResult();
102
    }
103
104
    /**
105
     * Remove a core from Solr
106
     *
107
     * @param string $core core name
108
     * @return StatusResult|null A result is successful
109
     */
110 1
    public function coreUnload($core)
111
    {
112 1
        $unload = $this->admin->createUnload();
113 1
        $unload->setCore($core);
114
115 1
        $this->admin->setAction($unload);
116 1
        $response = $this->client->coreAdmin($this->admin);
117
118 1
        return $response->getStatusResult();
119
    }
120
}
121