Test Failed
Pull Request — master (#128)
by Lucas
02:55
created

RecordsCounter::local()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Searchable;
15
16
use function in_array;
17
use Illuminate\Database\Eloquent\SoftDeletes;
18
use Algolia\ScoutExtended\Contracts\SearchableCountableContract;
19
20
/**
21
 * @internal
22
 */
23
final class RecordsCounter
24
{
25
    /**
26
     * Get the number of local searchable records of
27
     * the given searchable class.
28
     *
29
     * @param  string $searchable
30
     *
31
     * @return int
32
     */
33
    public function local(string $searchable): int
34
    {
35
        if (($instance = new $searchable) instanceof SearchableCountableContract) {
36
            $count = $instance->getSearchableCount();
37
        } else {
38
            $softDeletes = in_array(SoftDeletes::class, class_uses_recursive($searchable), true) && config('scout.soft_delete', false);
39
40
            $count = $searchable::query()->when($softDeletes, function ($query) {
41
                $query->withTrashed();
42
            })->count();
43
        }
44
45
        return (int) $count;
46
    }
47
48
    /**
49
     * Get the number of remote searchable records of
50
     * the given searchable class.
51
     *
52
     * @param  string $searchable
53
     *
54
     * @return int
55
     */
56
    public function remote(string $searchable): int
57
    {
58
        return (int) $searchable::search()->count();
59
    }
60
}
61