forked from jadell/neo4jphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateRelationship.php
102 lines (91 loc) · 2.03 KB
/
CreateRelationship.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace Everyman\Neo4j\Command;
use Everyman\Neo4j\Command,
Everyman\Neo4j\Client,
Everyman\Neo4j\Exception,
Everyman\Neo4j\Relationship,
Everyman\Neo4j\Node;
/**
* Create a relationship
*/
class CreateRelationship extends Command
{
protected $rel = null;
/**
* Set the relationship to drive the command
*
* @param Client $client
* @param Relationship $rel
*/
public function __construct(Client $client, Relationship $rel)
{
parent::__construct($client);
$this->rel = $rel;
}
/**
* Return the data to pass
*
* @return mixed
*/
protected function getData()
{
$end = $this->rel->getEndNode();
$type = $this->rel->getType();
if (!$end || !$end->hasId()) {
throw new Exception('No relationship end node specified');
} else if (!$type) {
throw new Exception('No relationship type specified');
}
$endUri = $this->getTransport()->getEndpoint().'/node/'.$end->getId();
$data = array(
'type' => $type,
'to' => $endUri,
);
$properties = $this->rel->getProperties();
if ($properties) {
$data['data'] = $properties;
}
return $data;
}
/**
* Return the transport method to call
*
* @return string
*/
protected function getMethod()
{
return 'post';
}
/**
* Return the path to use
*
* @return string
*/
protected function getPath()
{
$start = $this->rel->getStartNode();
if (!$start || !$start->hasId()) {
throw new Exception('No relationship start node specified');
}
return '/node/'.$start->getId().'/relationships';
}
/**
* Use the results
*
* @param integer $code
* @param array $headers
* @param array $data
* @return boolean true on success
* @throws Exception on failure
*/
protected function handleResult($code, $headers, $data)
{
if ((int)($code / 100) != 2) {
$this->throwException('Unable to create relationship', $code, $headers, $data);
}
$relId = $this->getEntityMapper()->getIdFromUri($headers['Location']);
$this->rel->setId($relId);
$this->getEntityCache()->setCachedEntity($this->rel);
return true;
}
}