Completed
Push — master ( da1cfa...dccc7f )
by Vladimir
03:06
created

IntentServiceProvider::register()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 10
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation;
6
7
use FondBot\Application\Config;
8
use League\Container\ServiceProvider\AbstractServiceProvider;
9
10
class IntentServiceProvider extends AbstractServiceProvider
11
{
12
    protected $provides = [
13
        IntentManager::class,
14
    ];
15
16
    /**
17
     * Use the register method to register items with the container via the
18
     * protected $this->container property or the `getContainer` method
19
     * from the ContainerAwareTrait.
20
     *
21
     * @return void
22
     */
23
    public function register(): void
24
    {
25 1
        $this->getContainer()->share(IntentManager::class, function () {
26
            /** @var Config $config */
27 1
            $config = $this->getContainer()->get(Config::class);
28
29
            /** @var array $intents */
30 1
            $intents = $config->get('intents', []);
31
32
            /** @var string $fallbackIntent */
33 1
            $fallbackIntent = $config->get('fallback_intent', FallbackIntent::class);
34
35 1
            $manager = new IntentManager();
36
37 1
            foreach ($intents as $intent) {
38 1
                $manager->add($this->getContainer()->get($intent));
39
            }
40
41 1
            $manager->setFallbackIntent($this->getContainer()->get($fallbackIntent));
42
43 1
            return $manager;
44 1
        });
45 1
    }
46
}
47