Completed
Push — master ( 06f997...84d5d5 )
by Vladimir
03:43
created

FondBot   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 24
dl 0
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A routes() 0 8 1
A intentsIn() 0 22 4
A intents() 0 5 1
A version() 0 3 1
A fallbackIntent() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot;
6
7
use SplFileInfo;
8
use ReflectionClass;
9
use Illuminate\Support\Str;
10
use FondBot\Conversation\Intent;
11
use Symfony\Component\Finder\Finder;
12
use Illuminate\Support\Facades\Route;
13
use FondBot\Foundation\Http\Middleware\InitializeKernel;
14
15
class FondBot
16
{
17
    /**
18
     * The registered intents.
19
     *
20
     * @var array
21
     */
22
    public static $intents = [];
23
24
    /**
25
     * Fallback intent.
26
     *
27
     * @var string
28
     */
29
    public static $fallbackIntent;
30
31
    /**
32
     * Get the current FondBot version.
33
     *
34
     * @return string
35
     */
36
    public static function version(): string
37
    {
38
        return '4.0.0';
39
    }
40
41
    /**
42
     * Register the given intents.
43
     *
44
     * @param  array $intents
45
     * @return static
46
     */
47
    public static function intents(array $intents)
48
    {
49
        static::$intents = array_merge(static::$intents, $intents);
50
51
        return new static;
52
    }
53
54
    /**
55
     * Register all of the intent classes in the given directory.
56
     *
57
     * @param  string $directory
58
     * @return void
59
     * @throws \ReflectionException
60
     */
61
    public static function intentsIn(string $directory): void
62
    {
63
        $namespace = app()->getNamespace();
0 ignored issues
show
introduced by
The method getNamespace() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

63
        $namespace = app()->/** @scrutinizer ignore-call */ getNamespace();
Loading history...
64
65
        $intents = [];
66
67
        /** @var SplFileInfo[] $files */
68
        $files = (new Finder)->in($directory)->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()) {
78
                $intents[] = $file;
79
            }
80
        }
81
82
        static::intents($intents);
83
    }
84
85
    /**
86
     * Register the given fallback intent.
87
     *
88
     * @param  string $intent
89
     * @return void
90
     */
91
    public static function fallbackIntent(string $intent): void
92
    {
93
        static::$fallbackIntent = $intent;
94
    }
95
96
    /**
97
     * Register the FondBot routes.
98
     *
99
     * @return void
100
     */
101
    public static function routes(): void
102
    {
103
        Route::middleware(InitializeKernel::class)
104
            ->namespace('FondBot\Foundation\Http\Controllers')
105
            ->group(function () {
106
                Route::group(['middleware' => 'fondbot.webhook'], function () {
107
                    Route::get('/fondbot/{channel}/{secret?}', 'WebhookController@store')->name('fondbot.webhook');
108
                    Route::post('/fondbot/{channel}/{secret?}', 'WebhookController@store')->name('fondbot.webhook');
109
                });
110
            });
111
    }
112
}
113