Completed
Push — master ( ed3a3e...3e13b9 )
by Vladimir
02:37
created

ConversationServiceProvider::boot()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 0
dl 0
loc 15
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Foundation\Providers;
6
7
use ReflectionClass;
8
use Illuminate\Support\Str;
9
use FondBot\Conversation\Intent;
10
use FondBot\Framework\Application;
11
use Symfony\Component\Finder\Finder;
12
use Illuminate\Support\ServiceProvider;
13
use FondBot\Conversation\FallbackIntent;
14
use FondBot\Conversation\SessionManager;
15
use Symfony\Component\Finder\SplFileInfo;
16
use FondBot\Contracts\Conversation\Manager;
17
use FondBot\Conversation\ConversationManager;
18
use Illuminate\Contracts\Cache\Repository as Cache;
19
20
/**
21
 * @property Application $app
22
 */
23
class ConversationServiceProvider extends ServiceProvider
24
{
25
    protected $intents = [];
26
    protected $fallbackIntent = FallbackIntent::class;
27
28
    /**
29
     * Register application services.
30
     */
31 83
    public function register(): void
32
    {
33 83
        $this->registerManager();
34 83
        $this->registerSessionManager();
35 83
    }
36
37
    /**
38
     * Boot application services.
39
     */
40 83
    public function boot(): void
41
    {
42
        /** @var Manager $manager */
43 83
        $manager = $this->app['conversation'];
44
45 83
        foreach ($this->intents as $intent) {
46
            $manager->registerIntent($intent);
47
        }
48
49 83
        $manager->registerFallbackIntent($this->fallbackIntent);
50
51 83
        if (method_exists($this, 'configure')) {
52
            $this->configure();
0 ignored issues
show
Bug introduced by
The method configure() does not seem to exist on object<FondBot\Foundatio...rsationServiceProvider>.

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...
53
        }
54 83
    }
55
56
    /**
57
     * Load intents from path.
58
     *
59
     * @param string $path
60
     */
61
    protected function load(string $path): void
62
    {
63
        $namespace = $this->app->getNamespace();
64
65
        /** @var SplFileInfo[] $files */
66
        $files = (new Finder)->in($path)->files();
67
68
        foreach ($files as $file) {
69
            $file = $namespace.str_replace(
70
                    ['/', '.php'],
71
                    ['\\', ''],
72
                    Str::after($file->getPathname(), app_path().DIRECTORY_SEPARATOR)
73
                );
74
75
            if (is_subclass_of($file, Intent::class) && !(new ReflectionClass($file))->isAbstract()) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \FondBot\Conversation\Intent::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
76
                $this->app['conversation']->registerIntent($file);
77
            }
78
        }
79
    }
80
81
    private function registerManager(): void
82
    {
83 83
        $this->app->singleton('conversation', function () {
84 83
            return new ConversationManager;
85 83
        });
86
87 83
        $this->app->alias('conversation', Manager::class);
88 83
    }
89
90
    private function registerSessionManager(): void
91
    {
92 83
        $this->app->singleton(SessionManager::class, function () {
93
            return new SessionManager($this->app, resolve(Cache::class));
94 83
        });
95
    }
96
}
97