Test Failed
Pull Request — master (#214)
by
unknown
05:52
created

SearchableFinder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 86
ccs 21
cts 23
cp 0.913
rs 10
c 3
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isSearchableModel() 0 3 1
A find() 0 6 2
A fromCommand() 0 9 3
A getProjectClasses() 0 18 4
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 18
    public function __construct($app)
46
    {
47 18
        $this->app = $app;
48 18
    }
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 18
    public function fromCommand(Command $command): array
58
    {
59 18
        $searchables = (array) $command->argument('searchable');
60
61 18
        if (empty($searchables) && empty($searchables = $this->find($command))) {
62
            throw new InvalidArgumentException('No searchable classes found.');
63
        }
64
65 18
        return $searchables;
66
    }
67
68
    /**
69
     * Get a list of searchable models.
70
     *
71
     * @return string[]
72
     */
73 3
    public function find(Command $command): array
74
    {
75 3
        $appNamespace = $this->app->getNamespace();
76
77
        return array_values(array_filter($this->getProjectClasses($command), function (string $class) use ($appNamespace) {
78 3
            return Str::startsWith($class, $appNamespace) && $this->isSearchableModel($class);
79 3
        }));
80
    }
81
82
    /**
83
     * @param  string $class
84
     *
85
     * @return bool
86
     */
87 3
    private function isSearchableModel($class): bool
88
    {
89 3
        return in_array(Searchable::class, class_uses_recursive($class), true);
90
    }
91
92
    /**
93
     * @return array
94
     */
95 3
    private function getProjectClasses(Command $command): array
96
    {
97 3
        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
                } catch (Error $e) {
104
                    // log a warning to the user and continue
105 1
                    $command->info("{$file} could not be inspected due to an error being thrown while loading it.");
106
                }
107
            }
108
109 1
            self::$declaredClasses = get_declared_classes();
110
        }
111
112 3
        return self::$declaredClasses;
113
    }
114
}
115