Passed
Push — master ( c9415f...63dfa1 )
by Vladimir
02:04
created

FondBot::routes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
13
class FondBot
14
{
15
    /**
16
     * The registered intents.
17
     *
18
     * @var array
19
     */
20
    public static $intents = [];
21
22
    /**
23
     * Fallback intent.
24
     *
25
     * @var string
26
     */
27
    public static $fallbackIntent;
28
29
    /**
30
     * Get the current FondBot version.
31
     *
32
     * @return string
33
     */
34
    public static function version(): string
35
    {
36
        return '4.0.0';
37
    }
38
39
    /**
40
     * Register the given intents.
41
     *
42
     * @param  array $intents
43
     * @return static
44
     */
45
    public static function intents(array $intents)
46
    {
47
        static::$intents = array_merge(static::$intents, $intents);
48
49
        return new static;
50
    }
51
52
    /**
53
     * Register all of the intent classes in the given directory.
54
     *
55
     * @param  string $directory
56
     * @return void
57
     * @throws \ReflectionException
58
     */
59
    public static function intentsIn(string $directory): void
60
    {
61
        $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

61
        $namespace = app()->/** @scrutinizer ignore-call */ getNamespace();
Loading history...
62
63
        $intents = [];
64
65
        /** @var SplFileInfo[] $files */
66
        $files = (new Finder)->in($directory)->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()) {
76
                $intents[] = $file;
77
            }
78
        }
79
80
        static::intents($intents);
81
    }
82
83
    /**
84
     * Register the given fallback intent.
85
     *
86
     * @param  string $intent
87
     * @return void
88
     */
89
    public static function fallbackIntent(string $intent): void
90
    {
91
        static::$fallbackIntent = $intent;
92
    }
93
}
94