Completed
Pull Request — develop (#241)
by ANTHONIUS
08:03
created

Manager::addDocument()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 8
nc 4
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license   MIT
7
 */
8
9
namespace Solr\Bridge;
10
11
12
use Solr\Exception\ServerException;
13
use Solr\Options\Connection as ConnectionOption;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
16
class Manager
17
{
18
    const SOLR_DATE_FORMAT  = 'Y-m-d\TH:i:s\Z';
19
    const SORT_ASCENDING    = 0;
20
    const SORT_DESCENDING   = 1;
21
22
    /**
23
     * @var ConnectionOption
24
     */
25
    protected $connectOption;
26
27
    public function __construct(ConnectionOption $connectOption)
28
    {
29
        $this->connectOption = $connectOption;
30
    }
31
32
    public function getClient($path='/solr')
33
    {
34
        $connectOption = $this->connectOption;
35
        $options = [
36
            'secure' => $connectOption->isSecure(),
37
            'hostname' => $connectOption->getHostname(),
38
            'port' => $connectOption->getPort(),
39
            'path' => $path,
40
            'login' => $connectOption->getUsername(),
41
            'password' => $connectOption->getPassword(),
42
        ];
43
44
        return new \SolrClient($options);
45
    }
46
47
    /**
48
     * @param \SolrInputDocument $document
49
     * @param string $path
50
     */
51
    public function addDocument(\SolrInputDocument $document,$path='/solr')
52
    {
53
        $client = $this->getClient($path);
54
        try{
55
            $client->addDocument($document);
56
            $client->commit();
57
            $client->optimize();
58
        }catch (\Exception $e){
59
            throw new ServerException('Can not add document to server!',$e->getCode(),$e);
60
        }
61
    }
62
    
63
    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...
64
    {
65
        return new self(
66
            $sl->get('Solr/Options/Connection')
0 ignored issues
show
Documentation introduced by
$sl->get('Solr/Options/Connection') is of type object|array, but the function expects a object<Solr\Options\Connection>.

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...
67
        );
68
    }
69
}