Issues (186)

src/Search/Services/IndexableService.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Search\Services;
4
5
use SilverStripe\Core\Extensible;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\Tests\MySQLDatabaseTest\Data;
0 ignored issues
show
The type SilverStripe\ORM\Tests\MySQLDatabaseTest\Data was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Checks if a DataObject is publically viewable thus able to be added or retrieved from a publically searchable index
12
 * Caching results because these checks may be done multiple times as there a few different code paths that search
13
 * results might follow in real-world search implementations
14
 */
15
class IndexableService
16
{
17
18
    use Injectable;
19
    use Extensible;
20
21
    protected $cache = [];
22
23
    public function clearCache(): void
24
    {
25
        $this->cache = [];
26
    }
27
28
    public function isIndexable(DataObject $obj): bool
29
    {
30
        // check if is a valid DataObject that has been persisted to the database
31
        if (is_null($obj) || !$obj->ID) {
32
            return false;
33
        }
34
35
        $key = $this->getCacheKey($obj);
36
        if (isset($this->cache[$key])) {
37
            return $this->cache[$key];
38
        }
39
40
        $value = true;
41
42
        // This will also call $obj->getShowInSearch() if it exists
43
        if (isset($obj->ShowInSearch) && !$obj->ShowInSearch) {
44
            $value = false;
45
        }
46
47
        $this->extend('updateIsIndexable', $obj, $value);
48
        $this->cache[$key] = $value;
49
        return $value;
50
    }
51
52
    protected function getCacheKey(DataObject $obj): string
53
    {
54
        $key = $obj->ClassName . '_' . $obj->ID;
55
        $this->extend('updateCacheKey', $obj, $key);
56
        return $key;
57
    }
58
}
59