Test Failed
Push — develop ( 63ed01...7218b8 )
by Nuno
05:28
created

SearchableFinder::__construct()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Helpers;
15
16
use function in_array;
17
use Illuminate\Support\Str;
18
use Laravel\Scout\Searchable;
19
use Illuminate\Console\Command;
20
use Symfony\Component\Finder\Finder;
21
use Illuminate\Foundation\Application;
22
use Symfony\Component\Console\Exception\InvalidArgumentException;
23
24
/**
25
 * @internal
26
 */
27
final class SearchableFinder
28
{
29
    /**
30
     * @var array
31
     */
32
    private static $declaredClasses;
33
34
    /**
35
     * @var \Illuminate\Foundation\Application
36
     */
37
    private $app;
38
39
    /**
40
     * SearchableModelsFinder constructor.
41
     *
42
     * @param \Illuminate\Foundation\Application $app
43
     *
44
     * @return void
45
     */
46 8
    public function __construct(Application $app)
47
    {
48 8
        $this->app = $app;
49 8
    }
50
51
    /**
52
     * Get a list of searchable models from the given command.
53
     *
54
     * @param \Illuminate\Console\Command $command
55
     *
56
     * @return array
57
     */
58 8
    public function fromCommand(Command $command): array
59
    {
60 8
        $searchables = (array) $command->argument('searchable');
61
62 8
        if (empty($searchables) && empty($searchables = $this->find())) {
63
            throw new InvalidArgumentException('No searchable models found. Please add the ['.Searchable::class.'] trait to a model.');
64
        }
65
66 8
        return $searchables;
67
    }
68
69
    /**
70
     * Get a list of searchable models.
71
     *
72
     * @return string[]
73
     */
74 2
    public function find(): array
75
    {
76 2
        $appNamespace = $this->app->getNamespace();
77
78
        return array_values(array_filter($this->getProjectClasses(), function (string $class) use ($appNamespace) {
79 2
            return Str::startsWith($class, $appNamespace) && $this->isSearchableModel($class);
80 2
        }));
81
    }
82
83
    /**
84
     * @param  string $class
85
     *
86
     * @return bool
87
     */
88 2
    private function isSearchableModel($class): bool
89
    {
90 2
        return in_array(Searchable::class, class_uses_recursive($class), true);
91
    }
92
93
    /**
94
     * @return array
95
     */
96 2
    private function getProjectClasses(): array
97
    {
98 2
        if (self::$declaredClasses === null) {
0 ignored issues
show
introduced by
The condition self::declaredClasses is always false. If self::declaredClasses can have other possible types, add them to src/Helpers/SearchableFinder.php:30
Loading history...
99 1
            $configFiles = Finder::create()->files()->name('*.php')->in($this->app->path());
100
101 1
            foreach ($configFiles->files() as $file) {
102 1
                require_once $file;
103
            }
104
105 1
            self::$declaredClasses = get_declared_classes();
106
        }
107
108 2
        return self::$declaredClasses;
109
    }
110
}
111