RecordsCounter::remote()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 Algolia\ScoutExtended\Contracts\SearchableCountableContract;
17
use Illuminate\Database\Eloquent\SoftDeletes;
18
use function in_array;
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 1
    public function local(string $searchable): int
34
    {
35 1
        if (($instance = new $searchable) instanceof SearchableCountableContract) {
36
            $count = $instance->getSearchableCount();
37
        } else {
38 1
            $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 1
            })->count();
43
        }
44
45 1
        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 1
    public function remote(string $searchable): int
57
    {
58 1
        return (int) $searchable::search()->count();
59
    }
60
}
61