Inserter::insertLog()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 4
crap 2
1
<?php
2
3
namespace tomzx\IRCStats;
4
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\NullLogger;
8
9
class Inserter implements LoggerAwareInterface
10
{
11
	/**
12
	 * @var \tomzx\IRCStats\DatabaseProxy
13
	 */
14
	protected $databaseProxy;
15
	/**
16
	 * @var \Psr\Log\LoggerInterface
17
	 */
18
	protected $logger;
19
20
	/**
21
	 * @param \tomzx\IRCStats\DatabaseProxy $databaseProxy
22
	 */
23
	public function __construct(DatabaseProxy $databaseProxy)
24
	{
25
		$this->databaseProxy = $databaseProxy;
26
		$this->logger = new NullLogger();
27
	}
28
29
	/**
30
	 * @return \Illuminate\Database\Connection
31
	 */
32
	protected function getDatabase()
33
	{
34
		return $this->databaseProxy->getConnection();
35
	}
36
37
	/**
38
	 * @param \Psr\Log\LoggerInterface $logger
39
	 * @return void
40
	 */
41
	public function setLogger(LoggerInterface $logger)
42
	{
43
		$this->logger = $logger;
44
	}
45
46
47
	public function insert($server, $channel, $nick, $message, $timestamp)
48
	{
49
		$targetNetwork = $this->insertServer($server);
50
		$targetChannel = $this->insertChannel($targetNetwork, $channel);
51
		$targetNick = $this->insertNick($targetNetwork, $nick);
52
		$this->insertLog($targetChannel, $targetNick, $message, $timestamp);
53
	}
54
55 View Code Duplication
	protected function insertServer($server)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
56
	{
57
		$db = $this->getDatabase();
58
		$targetNetwork = $db->table('networks')->select('id')->where('server', '=', $server)->first();
59
		if ($targetNetwork) {
60
			return $targetNetwork->id;
61
		}
62
63
		return $db->table('networks')->insertGetId([
64
			'server' => $server,
65
		]);
66
	}
67
68 View Code Duplication
	protected function insertChannel($targetNetwork, $channel)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
	{
70
		$db = $this->getDatabase();
71
		$targetChannel = $db->table('channels')->select('id')->where('network_id', '=', $targetNetwork)->where('channel', '=', $channel)->first();
72
		if ($targetChannel) {
73
			return $targetChannel->id;
74
		}
75
76
		return $db->table('channels')->insertGetId([
77
			'network_id' => $targetNetwork,
78
			'channel' => $channel,
79
		]);
80
	}
81
82 View Code Duplication
	protected function insertNick($targetNetwork, $nick)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
83
	{
84
		$db = $this->getDatabase();
85
		$targetNick = $db->table('nicks')->select('id')->where('network_id', '=', $targetNetwork)->where('nick', '=', $nick)->first();
86
		if ($targetNick) {
87
			return $targetNick->id;
88
		}
89
90
		return $db->table('nicks')->insertGetId([
91
			'network_id' => $targetNetwork,
92
			'nick' => $nick,
93
		]);
94
	}
95
96
	public function insertLog($targetChannel, $targetNick, $timestamp, $message)
97
	{
98
		$db = $this->getDatabase();
99
		$db->table('logs')->insert([
100
			'channel_id' => $targetChannel,
101
			'nick_id' => $targetNick,
102
			'timestamp' => date('Y-m-d H:i:s', $timestamp),
103
			'message' => $message,
104
		]);
105
	}
106
}
107