1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xsolve\SalesforceClient\Repository; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\ArrayTransformerInterface; |
6
|
|
|
use JMS\Serializer\SerializationContext; |
7
|
|
|
use Xsolve\SalesforceClient\Client\SalesforceClient; |
8
|
|
|
use Xsolve\SalesforceClient\Model\AbstractSObject; |
9
|
|
|
use Xsolve\SalesforceClient\Request\Create; |
10
|
|
|
use Xsolve\SalesforceClient\Request\Delete; |
11
|
|
|
use Xsolve\SalesforceClient\Request\Get; |
12
|
|
|
use Xsolve\SalesforceClient\Request\Update; |
13
|
|
|
|
14
|
|
|
class SObjectRepository implements SObjectRepositoryInterface |
15
|
|
|
{ |
16
|
|
|
const GROUP_UPDATE = 'update'; |
17
|
|
|
const GROUP_CREATE = 'create'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var SalesforceClient |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ArrayTransformerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $serializer; |
28
|
|
|
|
29
|
6 |
|
public function __construct(SalesforceClient $client, ArrayTransformerInterface $serializer) |
30
|
|
|
{ |
31
|
6 |
|
$this->client = $client; |
32
|
6 |
|
$this->serializer = $serializer; |
33
|
6 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
2 |
|
public function create(AbstractSObject $object) |
39
|
|
|
{ |
40
|
2 |
|
$response = $this->client->doRequest(new Create( |
41
|
2 |
|
$object::getSObjectName(), |
42
|
2 |
|
$this->serializer->toArray($object, SerializationContext::create()->setGroups([self::GROUP_CREATE])) |
43
|
|
|
)); |
44
|
|
|
|
45
|
2 |
|
if (!$response['success']) { |
46
|
1 |
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$object->setId($response['id']); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function delete(AbstractSObject $object) |
56
|
|
|
{ |
57
|
1 |
|
$this->client->doRequest(new Delete($object::getSObjectName(), $object->getId())); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
1 |
|
public function update(AbstractSObject $object) |
64
|
|
|
{ |
65
|
1 |
|
$this->client->doRequest(new Update( |
66
|
1 |
|
$object::getSObjectName(), |
67
|
1 |
|
$object->getId(), |
68
|
1 |
|
$this->serializer->toArray($object, SerializationContext::create()->setGroups([self::GROUP_UPDATE])->setSerializeNull(true)) |
69
|
|
|
)); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
2 |
|
public function find(string $class, string $id): AbstractSObject |
76
|
|
|
{ |
77
|
2 |
|
return $this->serializer->fromArray( |
78
|
2 |
|
$this->getFieldValues($class, $id), |
79
|
1 |
|
$class |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
2 |
|
public function getFieldValues(string $class, string $id, array $fields = []): array |
87
|
|
|
{ |
88
|
2 |
|
if (!is_a($class, AbstractSObject::class, true)) { |
89
|
1 |
|
throw new \RuntimeException(sprintf('%s should extend %s', $class, AbstractSObject::class)); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $this->client->doRequest(new Get($class::getSObjectName(), $id, $fields)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|