Completed
Push — master ( 4672bf...c09367 )
by Vladimir
02:46
created

ConversationServiceProvider::load()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 1
ccs 0
cts 11
cp 0
crap 20
rs 9.2
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
    public function register(): void
32
    {
33
        $this->registerManager();
34
        $this->registerSessionManager();
35
    }
36
37
    /**
38
     * Boot application services.
39
     *
40
     * @return void
41
     */
42
    public function boot(): void
43
    {
44
        /** @var Manager $manager */
45
        $manager = $this->app['conversation'];
46
47
        foreach ($this->intents as $intent) {
48
            $manager->registerIntent($intent);
49
        }
50
51
        $manager->registerFallbackIntent($this->fallbackIntent);
52
53
        if (method_exists($this, 'configure')) {
54
            $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...
55
        }
56
    }
57
58
    /**
59
     * Load intents from path.
60
     *
61
     * @param string $path
62
     */
63
    protected function load(string $path): void
64
    {
65
        $namespace = $this->app->getNamespace();
66
67
        /** @var SplFileInfo[] $files */
68
        $files = (new Finder)->in($path)->files();
69
70
        foreach ($files as $file) {
71
            $file = $namespace.str_replace(
72
                    ['/', '.php'],
73
                    ['\\', ''],
74
                    Str::after($file->getPathname(), app_path().DIRECTORY_SEPARATOR)
75
                );
76
77
            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...
78
                $this->app['conversation']->registerIntent($file);
79
            }
80
        }
81
    }
82
83
    private function registerManager(): void
84
    {
85
        $this->app->singleton('conversation', function () {
86
            return new ConversationManager;
87
        });
88
89
        $this->app->alias('conversation', Manager::class);
90
    }
91
92
    private function registerSessionManager(): void
93
    {
94
        $this->app->singleton(SessionManager::class, function () {
95
            return new SessionManager($this->app, resolve(Cache::class));
96
        });
97
    }
98
}
99