Inserter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 38.78 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 1
cbo 4
dl 38
loc 98
ccs 0
cts 65
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDatabase() 0 4 1
A setLogger() 0 4 1
A insert() 0 7 1
A insertServer() 12 12 2
A insertChannel() 13 13 2
A insertNick() 13 13 2
A insertLog() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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