Completed
Pull Request — develop (#241)
by ANTHONIUS
07:36
created

Manager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 72
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getClient() 0 14 1
A addDocument() 0 11 2
A factory() 0 6 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Solr\Bridge;
11
12
13
use Solr\Exception\ServerException;
14
use Solr\Options\ModuleOptions;
15
use Zend\ServiceManager\ServiceLocatorInterface;
16
17
/**
18
 * Manage connection with the SolrServer
19
 *
20
 * @author  Anthonius Munthi <[email protected]>
21
 * @since   0.27
22
 * @package Solr\Bridge
23
 */
24
class Manager
25
{
26
    const SOLR_DATE_FORMAT  = 'Y-m-d\TH:i:s\Z';
27
    const SORT_ASCENDING    = 0;
28
    const SORT_DESCENDING   = 1;
29
30
    /**
31
     * @var ModuleOptions
32
     */
33
    protected $connectOption;
34
35
    /**
36
     * Manager constructor.
37
     * @param ModuleOptions $connectOption
38
     */
39
    public function __construct(ModuleOptions $connectOption)
40
    {
41
        $this->connectOption = $connectOption;
42
    }
43
44
    /**
45
     * Get \SolrClient with custom path option
46
     *
47
     * @param string $path
48
     * @return \SolrClient
49
     */
50
    public function getClient($path='/solr')
51
    {
52
        $connectOption = $this->connectOption;
53
        $options = [
54
            'secure' => $connectOption->isSecure(),
55
            'hostname' => $connectOption->getHostname(),
56
            'port' => $connectOption->getPort(),
57
            'path' => $path,
58
            'login' => $connectOption->getUsername(),
59
            'password' => $connectOption->getPassword(),
60
        ];
61
62
        return new \SolrClient($options);
63
    }
64
65
    /**
66
     * Add new document into Solr server
67
     *
68
     * @param \SolrInputDocument $document
69
     * @param string $path
70
     * @throws ServerException When failed adding document to server
71
     */
72
    public function addDocument(\SolrInputDocument $document,$path='/solr')
73
    {
74
        $client = $this->getClient($path);
75
        try{
76
            $client->addDocument($document);
77
            $client->commit();
78
            $client->optimize();
79
        }catch (\Exception $e){
80
            throw new ServerException('Can not add document to server!',$e->getCode(),$e);
81
        }
82
    }
83
84
    /**
85
     * Create new instance for Solr\Manager
86
     * @param ServiceLocatorInterface $sl
87
     * @return Manager
88
     */
89
    static public function factory(ServiceLocatorInterface $sl)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
90
    {
91
        return new self(
92
            $sl->get('Solr/Options/Module')
0 ignored issues
show
Documentation introduced by
$sl->get('Solr/Options/Module') is of type object|array, but the function expects a object<Solr\Options\ModuleOptions>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
        );
94
    }
95
}