Completed
Push — master ( 4510f4...5094ff )
by Vladimir
09:59
created

ConversationServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 51.85%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 14
cts 27
cp 0.5185
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A boot() 0 15 3
A load() 0 19 4
A registerManager() 0 8 1
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 Symfony\Component\Finder\SplFileInfo;
15
use FondBot\Contracts\Conversation\Manager;
16
use FondBot\Conversation\ConversationManager;
17
use Illuminate\Contracts\Cache\Repository as Cache;
18
19
/**
20
 * @property Application $app
21
 */
22
class ConversationServiceProvider extends ServiceProvider
23
{
24
    protected $intents = [];
25
    protected $fallbackIntent = FallbackIntent::class;
26
27
    /**
28
     * Register application services.
29
     */
30 88
    public function register(): void
31
    {
32 88
        $this->registerManager();
33 88
    }
34
35
    /**
36
     * Boot application services.
37
     */
38 88
    public function boot(): void
39
    {
40
        /** @var Manager $manager */
41 88
        $manager = $this->app['conversation'];
42
43 88
        foreach ($this->intents as $intent) {
44
            $manager->registerIntent($intent);
45
        }
46
47 88
        $manager->registerFallbackIntent($this->fallbackIntent);
48
49 88
        if (method_exists($this, 'configure')) {
50
            $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...
51
        }
52 88
    }
53
54
    /**
55
     * Load intents from path.
56
     *
57
     * @param string $path
58
     */
59
    protected function load(string $path): void
60
    {
61
        $namespace = $this->app->getNamespace();
62
63
        /** @var SplFileInfo[] $files */
64
        $files = (new Finder)->in($path)->files();
65
66
        foreach ($files as $file) {
67
            $file = $namespace.str_replace(
68
                    ['/', '.php'],
69
                    ['\\', ''],
70
                    Str::after($file->getPathname(), app_path().DIRECTORY_SEPARATOR)
71
                );
72
73
            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...
74
                $this->app['conversation']->registerIntent($file);
75
            }
76
        }
77
    }
78
79
    private function registerManager(): void
80
    {
81 88
        $this->app->singleton('conversation', function () {
82 88
            return new ConversationManager($this->app, $this->app[Cache::class]);
83 88
        });
84
85 88
        $this->app->alias('conversation', Manager::class);
86 88
    }
87
}
88