Completed
Pull Request — develop (#291)
by
unknown
08:11
created

ConsoleController::createProgressBar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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\Controller;
11
12
13
use Core\Console\ProgressBar;
14
use Jobs\Repository\Job as JobRepository;
15
use SolrClient;
16
use Zend\Mvc\Controller\AbstractActionController;
17
18
/**
19
 * @author Anthonius Munthi <[email protected]>
20
 * @author Miroslav Fedeleš <[email protected]>
21
 * @since 0.26
22
 * @package Solr\Controller
23
 */
24
class ConsoleController extends AbstractActionController
25
{
26
    
27
    /**
28
     * @var SolrClient
29
     */
30
    protected $solrClient;
31
    
32
    /**
33
     * @var JobRepository
34
     */
35
    protected $jobRepository;
36
    
37
    /**
38
     * @var callable
39
     */
40
    protected $progressBarFactory;
41
    
42
    /**
43
     * @param SolrClient $solrClient
44
     * @param JobRepository $jobRepository
45
     * @param callable $progressBarFactory
46
     * @since 0.27
47
     */
48
    public function __construct(SolrClient $solrClient, JobRepository $jobRepository, callable $progressBarFactory)
49
    {
50
        $this->solrClient = $solrClient;
51
        $this->jobRepository = $jobRepository;
52
        $this->progressBarFactory = $progressBarFactory;
53
    }
54
55
    public function activeJobIndexAction()
56
    {
57
        $jobs = $this->jobRepository->findActiveJob();
58
        $count = $jobs->count();
59
        
60
        // check if there is any active job
61
        if (0 === $count) {
62
            return 'There is no active job'.PHP_EOL;
63
        }
64
        
65
        $i = 1;
66
        $progressBarFactory = $this->progressBarFactory;
67
        $progressBar = $progressBarFactory($count);
68
        $entityToDocument = new \Solr\Filter\EntityToDocument\Job();
69
        
70
        // add jobs in the Solr index
71
        foreach ($jobs as $job) {
72
            $document = $entityToDocument->filter($job);
0 ignored issues
show
Documentation introduced by
$job is of type object|array, but the function expects a object<Jobs\Entity\Job>.

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...
73
            $this->solrClient->addDocument($document);
74
            $progressBar->update($i, 'Job ' . $i . ' / ' . $count);
75
            $i++;
76
        }
77
        
78
        $this->solrClient->commit();
79
        $this->solrClient->optimize();
80
81
        return PHP_EOL;
82
    }
83
    
84
    /**
85
	 * @return callable
86
	 */
87
	public function getProgressBarFactory()
88
	{
89
		return $this->progressBarFactory;
90
	}
91
92
}