TchatBot::setBrain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Posibrain;
3
4
use Monolog\Logger;
5
include_once (__DIR__ . '/../tools.php');
6
7
/**
8
 *
9
 * @author Fylhan (http://fylhan.la-bnbox.fr)
10
 * @license LGPL-2.1+
11
 */
12
class TchatBot implements ITchatBot
13
{
14
15
	private static $logger = NULL;
16
17
	private $identity;
18
19
	private $positrons;
20
21
	public function __construct($id = '', $lang = '', $params = array())
22
	{
23
		// Logger
24 View Code Duplication
		if (NULL == self::$logger) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
			self::$logger = new Logger(__CLASS__);
26
			if (! empty($params) && isset($params['loggerHandler'])) {
27
				self::$logger->pushHandler($params['loggerHandler']);
28
			}
29
		}
30
		
31
		// Config
32
		$this->identity = new TchatBotIdentity($id, $lang, $params);
33
		
34
		// Brain Manager
35
		$this->positrons = new Positroner($this->identity, $params);
0 ignored issues
show
Documentation introduced by
$this->identity is of type object<Posibrain\TchatBotIdentity>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
		$this->positrons->loadPositrons(@$params['positrons'], $this->identity, $params);
0 ignored issues
show
Documentation introduced by
$this->identity is of type object<Posibrain\TchatBotIdentity>, but the function expects a object<Posibrain\Config>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
	}
38
39
	public function isTriggered($userMessage, $userName = '', $dateTime = 0)
40
	{
41
		$request = new TchatMessage($userMessage, $userName, $dateTime);
42
		return $this->positrons->isBotTriggered($request);
43
	}
44
45
	public function generateAnswer($userMessage, $userName = '', $dateTime = 0)
46
	{
47
		$request = new TchatMessage($userMessage, $userName, $dateTime);
48
		if (!$this->positrons->isBotTriggered($request)) {
49
			return null;
50
		}
51
		$request = $this->positrons->analyseRequest($request);
52
		$memory = $this->positrons->loadMemory($request);
53
		$answer = $this->positrons->generateSymbolicAnswer($request, $memory);
54
		$answer = $this->positrons->provideMeaning($request, $memory, $answer);
55
		$answer = $this->positrons->beautifyAnswer($request, $memory, $answer);
56
		if (null == $answer || ('' == $answer->getMessage() && '' == $answer->getName())) {
57
			$answer = new TchatMessage('Ssqdijoezf ? Jkfd.', 'QTzbn');
58
		}
59
		return $answer->toArray();
60
	}
61
62
	public function getIdentity()
63
	{
64
		return $this->identity;
65
	}
66
67
	public function setConfig($config)
68
	{
69
		$this->identity = $config;
70
	}
71
72
	public function setBrain($brain)
73
	{
74
		$this->positrons = $brain;
75
	}
76
}
77
78