Completed
Push — master ( ddcd57...befb63 )
by Adam
12:31
created

RebuildModels   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 98
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getModels() 0 12 5
A setModels() 0 4 1
A output() 0 6 2
A rebuild() 0 11 2
A loopModels() 0 6 2
A rebuildModel() 0 16 2
A chunk() 0 9 2
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Laravel;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
use Symfony\Component\Console\Output\NullOutput;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class RebuildModels
12
 * @package BestServedCold\LaravelZendSearch\Laravel
13
 */
14
class RebuildModels
15
{
16
    private $progressBar;
17
    private $store;
18
    private $output;
19
    private $models = [];
20
21 5
    public function __construct(ProgressBar $progressBar, Store $store, OutputInterface $output)
22
    {
23 5
        $this->progressBar = $progressBar;
24 5
        $this->store = $store;
25 5
        $this->output = $output;
26 5
        $this->models = $this->getModels();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getModels() can also be of type boolean. However, the property $models is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
27 5
    }
28
29
    /**
30
     * @return array|bool
31
     */
32 5
    private function getModels()
33
    {
34 5
        $models = [];
35
36 5
        foreach (get_declared_classes() as $class) {
37 5
            if (is_subclass_of($class, Model::class) && method_exists($class, 'searchFields')) {
1 ignored issue
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Illuminate\Database\Eloquent\Model::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
38 5
                $models[] = $class;
39 5
            }
40 5
        }
41
42 5
        return empty($models) ? [] : $models;
43
    }
44
45 1
    public function setModels(array $models = [])
46
    {
47 1
        $this->models = $models;
48 1
    }
49
50 5
    private function output($type, $string)
51
    {
52 5
        if (! $this->output instanceof NullOutput) {
53 4
            $this->output->$type($string);
54 4
        }
55 5
    }
56
57 5
    public function rebuild()
58
    {
59 5
        if (empty($this->models)) {
60 1
            $this->output('error', 'No models configured for search.');
61 1
        }
62
63 5
        $this->loopModels();
64
65 5
        $this->output('comment', PHP_EOL . 'Search engine rebuild complete.');
66 5
        return $this->output;
67
    }
68
69
    /**
70
     * @param array $models
0 ignored issues
show
Bug introduced by
There is no parameter named $models. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
71
     */
72 5
    private function loopModels()
73
    {
74 5
        foreach ($this->models as $modelName) {
75 4
            $this->rebuildModel(new $modelName);
76 5
        }
77 5
    }
78
79
    /**
80
     * @param Model $model
81
     */
82 4
    private function rebuildModel(Model $model)
83
    {
84 4
        $this->output('comment', 'Creating index for model [' . $model->getTable() . ']');
85
86 4
        if ($model->count() === 0) {
87 2
            $this->output(
88 2
                'comment',
89 2
                'No records for model [' . $model->getTable() . '].'
90 2
            );
91 2
            return;
92
        }
93
94 2
        $this->progressBar->start($model->count());
95 2
        $this->chunk($model);
96 2
        $this->progressBar->finish();
97 2
    }
98
99
    /**
100
     * @param Model $object
101
     */
102
    private function chunk(Model $object)
103
    {
104 2
        $object->chunk(1000, function ($chunk) {
105 1
            foreach ($chunk as $record) {
106 1
                $this->store->insertModel($record, false);
107 1
                $this->progressBar->advance();
108 1
            }
109 2
        });
110 2
    }
111
}
112