Completed
Pull Request — master (#214)
by
unknown
08:31 queued 04:02
created

SearchableFinder::find()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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