CoreAdminTrait::coreCreate()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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