|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Donut Social Network - Yet another experimental social network. |
|
5
|
|
|
* Copyright (C) 2016-2017, Dejan Angelov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of Donut Social Network. |
|
8
|
|
|
* |
|
9
|
|
|
* Donut Social Network is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* Donut Social Network is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with Donut Social Network. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Donut Social Network |
|
23
|
|
|
* @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]> |
|
24
|
|
|
* @license https://github.com/angelov/donut/blob/master/LICENSE |
|
25
|
|
|
* @author Dejan Angelov <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Angelov\Donut\Friendships\FriendshipsRecorder; |
|
29
|
|
|
|
|
30
|
|
|
use GraphAware\Neo4j\Client\Client; |
|
31
|
|
|
use Angelov\Donut\Friendships\Friendship; |
|
32
|
|
|
use Angelov\Donut\Users\User; |
|
33
|
|
|
|
|
34
|
|
|
class Neo4jFriendshipsRecorder implements FriendshipsRecorderInterface |
|
35
|
|
|
{ |
|
36
|
|
|
private $client; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(Client $client) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->client = $client; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function recordCreated(Friendship $friendship): void |
|
44
|
|
|
{ |
|
45
|
|
|
// @todo handle neo4j exceptions |
|
46
|
|
|
|
|
47
|
|
|
$user = $friendship->getUser(); |
|
48
|
|
|
$friend = $friendship->getFriend(); |
|
49
|
|
|
|
|
50
|
|
|
$this->createUserNode($user); |
|
51
|
|
|
$this->createUserNode($friend); |
|
52
|
|
|
|
|
53
|
|
|
$this->createFriendshipRelationships($user, $friend); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function createUserNode(User $user) : void |
|
57
|
|
|
{ |
|
58
|
|
|
$query = 'MERGE (n:User {id: {id}, name: {name}})'; |
|
59
|
|
|
|
|
60
|
|
|
$this->client->run($query, ['id' => $user->getId(), 'name' => $user->getName()]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function createFriendshipRelationships(User $first, User $second) : void |
|
64
|
|
|
{ |
|
65
|
|
|
$query = ' |
|
66
|
|
|
MATCH |
|
67
|
|
|
(first:User {id:{first}}), |
|
68
|
|
|
(second:User {id:{second}}) |
|
69
|
|
|
CREATE |
|
70
|
|
|
(first)-[:FRIEND]->(second) |
|
71
|
|
|
'; |
|
72
|
|
|
|
|
73
|
|
|
$this->client->run($query, [ |
|
74
|
|
|
'first' => $first->getId(), |
|
75
|
|
|
'second' => $second->getId() |
|
76
|
|
|
]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function recordDeleted(Friendship $friendship): void |
|
80
|
|
|
{ |
|
81
|
|
|
// @todo handle exceptions |
|
82
|
|
|
|
|
83
|
|
|
$first = $friendship->getUser(); |
|
84
|
|
|
$second = $friendship->getFriend(); |
|
85
|
|
|
|
|
86
|
|
|
$query = ' |
|
87
|
|
|
MATCH |
|
88
|
|
|
(u:User {id: {first}}), |
|
89
|
|
|
(r:User {id: {second}}), |
|
90
|
|
|
(u)-[f:FRIEND]-(r) |
|
91
|
|
|
DELETE f |
|
92
|
|
|
'; |
|
93
|
|
|
|
|
94
|
|
|
$this->client->run($query, [ |
|
95
|
|
|
'first' => $first->getId(), |
|
96
|
|
|
'second' => $second->getId() |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|