|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace databoxtech\multisocial; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use databoxtech\multisocial\adapter\SocialAdapter; |
|
8
|
|
|
|
|
9
|
|
|
class MultiSocial |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var SocialAdapter[] |
|
14
|
|
|
*/ |
|
15
|
|
|
private $adapters = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var SocialFactory |
|
19
|
|
|
*/ |
|
20
|
|
|
private $factory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* MultiSocial constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* Config object is expected to be formatted as below, |
|
26
|
|
|
* [ |
|
27
|
|
|
* FacebookAdapter::class => [ |
|
28
|
|
|
* 'app_id' => '', |
|
29
|
|
|
* 'app_secret' => '', |
|
30
|
|
|
* 'access_token' => '', |
|
31
|
|
|
* 'page_id' => '', |
|
32
|
|
|
* ] |
|
33
|
|
|
* ] |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $config |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function __construct(array $config) |
|
38
|
|
|
{ |
|
39
|
4 |
|
$this->factory = new SocialFactory(); |
|
40
|
|
|
|
|
41
|
4 |
|
foreach ($config as $adapterName => $adapterConfig){ |
|
42
|
1 |
|
$name = strtolower($adapterName); |
|
43
|
1 |
|
$this->adapters[$name] = $this->factory->create($adapterName, $adapterConfig); |
|
44
|
|
|
} |
|
45
|
4 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $adapterName |
|
49
|
|
|
* @param SocialAdapter $adapter |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function addAdapter($adapterName, $adapter){ |
|
52
|
2 |
|
$this->adapters[$adapterName] = $adapter; |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $adapterName |
|
57
|
|
|
* @return SocialAdapter|null |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function getAdapter($adapterName){ |
|
60
|
2 |
|
$name = strtolower($adapterName); |
|
61
|
2 |
|
return isset($this->adapters[$name]) ? $this->adapters[$name] : null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Post specified post to all configured social platforms |
|
67
|
|
|
* |
|
68
|
|
|
* @param Post $post post to be published |
|
69
|
|
|
* @return array references of posts published |
|
70
|
|
|
* @throws exception\SocialException |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function post($post){ |
|
73
|
1 |
|
$references = []; |
|
74
|
1 |
|
foreach ($this->adapters as $adapterName => $adapter){ |
|
75
|
1 |
|
$references[$adapterName] = $adapter->post($post); |
|
76
|
|
|
} |
|
77
|
1 |
|
return $references; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |