Passed
Push — feat/searchindex-macro ( e3af89 )
by Chloe
12:05
created

ScoutExtendedServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 41
c 5
b 0
f 0
dl 0
loc 105
ccs 40
cts 42
cp 0.9524
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A boot() 0 3 1
A registerCommands() 0 11 2
A registerBinds() 0 36 1
A registerMacros() 0 14 2
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;
15
16
use Algolia\AlgoliaSearch\SearchClient;
17
use Illuminate\Support\ServiceProvider;
18
use Laravel\Scout\ScoutServiceProvider;
19
use Algolia\ScoutExtended\Jobs\UpdateJob;
20
use Algolia\AlgoliaSearch\AnalyticsClient;
21
use Algolia\ScoutExtended\Engines\AlgoliaEngine;
22
use Algolia\ScoutExtended\Managers\EngineManager;
23
use Algolia\ScoutExtended\Helpers\SearchableFinder;
24
use Algolia\ScoutExtended\Console\Commands\SyncCommand;
25
use Algolia\ScoutExtended\Console\Commands\FlushCommand;
26
use Algolia\ScoutExtended\Searchable\AggregatorObserver;
27
use Algolia\ScoutExtended\Console\Commands\ImportCommand;
28
use Algolia\ScoutExtended\Console\Commands\StatusCommand;
29
use Algolia\ScoutExtended\Console\Commands\OptimizeCommand;
30
use Algolia\ScoutExtended\Console\Commands\ReImportCommand;
31
use Algolia\ScoutExtended\Console\Commands\MakeAggregatorCommand;
32
33
final class ScoutExtendedServiceProvider extends ServiceProvider
34
{
35
    /**
36
     * {@inheritdoc}
37
     */
38 60
    public function boot(): void
39
    {
40 60
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'algolia');
0 ignored issues
show
Bug introduced by
The method loadViewsFrom() does not exist on Algolia\ScoutExtended\ScoutExtendedServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $this->/** @scrutinizer ignore-call */ 
41
               loadViewsFrom(__DIR__.'/../resources/views', 'algolia');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41 60
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 60
    public function register(): void
47
    {
48 60
        $this->app->register(ScoutServiceProvider::class);
0 ignored issues
show
Unused Code introduced by
The call to Tests\Laravel\App::register() has too many arguments starting with Laravel\Scout\ScoutServiceProvider::class. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $this->app->/** @scrutinizer ignore-call */ 
49
                    register(ScoutServiceProvider::class);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
50 60
        $this->registerBinds();
51 60
        $this->registerCommands();
52 60
        $this->registerMacros();
53 60
    }
54
55
    /**
56
     * Binds Algolia services into the container.
57
     *
58
     * @return void
59
     */
60 60
    private function registerBinds(): void
61
    {
62
        $this->app->bind(Algolia::class, function () {
0 ignored issues
show
Bug introduced by
The method bind() does not exist on Tests\Laravel\App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        $this->app->/** @scrutinizer ignore-call */ 
63
                    bind(Algolia::class, function () {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63 23
            return new Algolia($this->app);
0 ignored issues
show
Bug introduced by
$this->app of type Tests\Laravel\App is incompatible with the type Illuminate\Contracts\Container\Container expected by parameter $container of Algolia\ScoutExtended\Algolia::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            return new Algolia(/** @scrutinizer ignore-type */ $this->app);
Loading history...
64 60
        });
65
66 60
        $this->app->alias(Algolia::class, 'algolia');
0 ignored issues
show
Bug introduced by
The method alias() does not exist on Tests\Laravel\App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $this->app->/** @scrutinizer ignore-call */ 
67
                    alias(Algolia::class, 'algolia');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
        $this->app->singleton(EngineManager::class, function ($app) {
0 ignored issues
show
Bug introduced by
The method singleton() does not exist on Tests\Laravel\App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $this->app->/** @scrutinizer ignore-call */ 
69
                    singleton(EngineManager::class, function ($app) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69 55
            return new EngineManager($app);
70 60
        });
71
72 60
        $this->app->alias(EngineManager::class, \Laravel\Scout\EngineManager::class);
73
74
        $this->app->bind(AlgoliaEngine::class, function (): AlgoliaEngine {
75 53
            return $this->app->make(\Laravel\Scout\EngineManager::class)->createAlgoliaDriver();
0 ignored issues
show
Bug introduced by
The method make() does not exist on Tests\Laravel\App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            return $this->app->/** @scrutinizer ignore-call */ make(\Laravel\Scout\EngineManager::class)->createAlgoliaDriver();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76 60
        });
77
78 60
        $this->app->alias(AlgoliaEngine::class, 'algolia.engine');
79
        $this->app->bind(SearchClient::class, function (): SearchClient {
80 52
            return $this->app->make('algolia.engine')->getClient();
81 60
        });
82
83 60
        $this->app->alias(SearchClient::class, 'algolia.client');
84
85
        $this->app->bind(AnalyticsClient::class, function (): AnalyticsClient {
86 1
            return AnalyticsClient::create(config('scout.algolia.id'), config('scout.algolia.secret'));
87 60
        });
88
89 60
        $this->app->alias(AnalyticsClient::class, 'algolia.analytics');
90
91 60
        $this->app->singleton(AggregatorObserver::class, AggregatorObserver::class);
92 60
        $this->app->bind(\Laravel\Scout\Builder::class, Builder::class);
93
94
        $this->app->bind(SearchableFinder::class, function () {
95 17
            return new SearchableFinder($this->app);
0 ignored issues
show
Bug introduced by
$this->app of type Tests\Laravel\App is incompatible with the type Illuminate\Contracts\Foundation\Application expected by parameter $app of Algolia\ScoutExtended\He...leFinder::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
            return new SearchableFinder(/** @scrutinizer ignore-type */ $this->app);
Loading history...
96 60
        });
97 60
    }
98
99
    /**
100
     * Register artisan commands.
101
     *
102
     * @return void
103
     */
104 60
    private function registerCommands(): void
105
    {
106 60
        if ($this->app->runningInConsole()) {
0 ignored issues
show
Bug introduced by
The method runningInConsole() does not exist on Tests\Laravel\App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        if ($this->app->/** @scrutinizer ignore-call */ runningInConsole()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107 60
            $this->commands([
0 ignored issues
show
Bug introduced by
The method commands() does not exist on Algolia\ScoutExtended\ScoutExtendedServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            $this->/** @scrutinizer ignore-call */ 
108
                   commands([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108 60
                MakeAggregatorCommand::class,
109
                ImportCommand::class,
110
                FlushCommand::class,
111
                OptimizeCommand::class,
112
                ReImportCommand::class,
113
                StatusCommand::class,
114
                SyncCommand::class,
115
            ]);
116
        }
117 60
    }
118
119
    /**
120
     * Register macros.
121
     *
122
     * @return void
123
     */
124 60
    private function registerMacros(): void
125
    {
126
        \Illuminate\Database\Eloquent\Builder::macro('transform', function (array $array, array $transformers = null) {
127 15
            foreach ($transformers ?? UpdateJob::getTransformers() as $transformer) {
128 15
                $array = app($transformer)->transform($this->getModel(), $array);
0 ignored issues
show
Bug introduced by
The method transform() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
                $array = app($transformer)->/** @scrutinizer ignore-call */ transform($this->getModel(), $array);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getModel() does not exist on Algolia\ScoutExtended\ScoutExtendedServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
                $array = app($transformer)->transform($this->/** @scrutinizer ignore-call */ getModel(), $array);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
            }
130
131 15
            return $array;
132 60
        });
133
134
        \Illuminate\Database\Eloquent\Builder::macro('getSearchableIndex', function () {
135
            $client = $this->getModel()->searchableUsing()->getClient();
136
137
            return $client->initIndex($this->getModel()->searchableAs());
138 60
        });
139 60
    }
140
}
141