RecordsCounter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 36
ccs 7
cts 9
cp 0.7778
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A local() 0 13 3
A remote() 0 3 1
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