1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tomzx\IRCStats; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
6
|
|
|
|
7
|
|
|
class Parser { |
8
|
|
|
/** |
9
|
|
|
* @var \tomzx\IRCStats\DatabaseProxy |
10
|
|
|
*/ |
11
|
|
|
protected $databaseProxy; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param \tomzx\IRCStats\DatabaseProxy $databaseProxy |
15
|
|
|
*/ |
16
|
1 |
|
public function __construct(DatabaseProxy $databaseProxy) |
17
|
|
|
{ |
18
|
1 |
|
$this->databaseProxy = $databaseProxy; |
19
|
1 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return \Illuminate\Database\Connection |
23
|
|
|
*/ |
24
|
1 |
|
protected function getConnection() |
25
|
|
|
{ |
26
|
1 |
|
return $this->databaseProxy->getConnection(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $line |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
1 |
|
public function parseLine(array $line) |
34
|
|
|
{ |
35
|
1 |
|
$db = $this->getConnection(); |
36
|
|
|
|
37
|
1 |
|
$server = $line['server']; |
38
|
1 |
|
$channel = $line['channel']; |
39
|
1 |
|
$nick = $line['nick']; |
40
|
1 |
|
$timestamp = $line['timestamp']; |
41
|
1 |
|
$message = $line['message']; |
42
|
|
|
|
43
|
1 |
|
$networkId = $this->getNetworkId($db, $server); |
44
|
1 |
|
$channelId = $this->getChannelId($db, $networkId, $channel); |
45
|
1 |
|
$nickId = $this->getNickId($db, $channelId, $nick); |
46
|
|
|
|
47
|
1 |
|
$db->table('logs')->insert([ |
48
|
1 |
|
'channel_id' => $channelId, |
49
|
1 |
|
'nick_id' => $nickId, |
50
|
1 |
|
'timestamp' => $timestamp, |
51
|
1 |
|
'message' => $message, |
52
|
1 |
|
]); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param \Illuminate\Database\Connection $db |
57
|
|
|
* @param string $server |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
1 |
View Code Duplication |
protected function getNetworkId(Connection $db, $server) |
|
|
|
|
61
|
|
|
{ |
62
|
1 |
|
$targetNetwork = $db->table('networks') |
63
|
1 |
|
->select('id') |
64
|
1 |
|
->where('server', '=', $server) |
65
|
1 |
|
->first(); |
66
|
|
|
|
67
|
1 |
|
if ($targetNetwork) { |
68
|
1 |
|
return $targetNetwork['id']; |
69
|
|
|
} else { |
70
|
|
|
return $db->table('networks')->insertGetId([ |
71
|
|
|
'server' => $server, |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param \Illuminate\Database\Connection $db |
78
|
|
|
* @param int $networkId |
79
|
|
|
* @param string $channel |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
1 |
View Code Duplication |
protected function getChannelId(Connection $db, $networkId, $channel) |
|
|
|
|
83
|
|
|
{ |
84
|
1 |
|
$targetChannel = $db->table('channels') |
85
|
1 |
|
->select('id') |
86
|
1 |
|
->where('network_id', '=', $networkId) |
87
|
1 |
|
->where('channel', '=', $channel) |
88
|
1 |
|
->first(); |
89
|
|
|
|
90
|
1 |
|
if ($targetChannel) { |
91
|
1 |
|
return $targetChannel['id']; |
92
|
|
|
} else { |
93
|
|
|
return $db->table('channels')->insertGetId([ |
94
|
|
|
'network_id' => $networkId, |
95
|
|
|
'channel' => $channel, |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \Illuminate\Database\Connection $db |
102
|
|
|
* @param int $channelId |
103
|
|
|
* @param string $nick |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
1 |
View Code Duplication |
protected function getNickId(Connection $db, $channelId, $nick) |
|
|
|
|
107
|
|
|
{ |
108
|
1 |
|
$targetNick = $db->table('nicks') |
109
|
1 |
|
->select('id') |
110
|
1 |
|
->where('channel_id', '=', $channelId) |
111
|
1 |
|
->where('nick', '=', $nick) |
112
|
1 |
|
->first(); |
113
|
|
|
|
114
|
1 |
|
if ($targetNick) { |
115
|
1 |
|
return $targetNick['id']; |
116
|
|
|
} else { |
117
|
|
|
return $db->table('nicks')->insertGetId([ |
118
|
|
|
'channel_id' => $channelId, |
119
|
|
|
'nick' => $nick, |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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.