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
|
2 |
|
public function __construct(array $config) |
38
|
|
|
{ |
39
|
2 |
|
$this->factory = new SocialFactory(); |
40
|
|
|
|
41
|
2 |
|
foreach ($config as $adapterName => $adapterConfig){ |
42
|
1 |
|
$name = strtolower($adapterName); |
43
|
1 |
|
$this->adapters[$name] = $this->factory->create($adapterName, $adapterConfig); |
44
|
|
|
} |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $adapterName |
49
|
|
|
* @return SocialAdapter|null |
50
|
|
|
*/ |
51
|
1 |
|
public function getAdapter($adapterName){ |
52
|
1 |
|
$name = strtolower($adapterName); |
53
|
1 |
|
return isset($this->adapters[$name]) ? $this->adapters[$name] : null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Post specified post to all configured social platforms |
59
|
|
|
* |
60
|
|
|
* @param Post $post post to be published |
61
|
|
|
* @return array references of posts published |
62
|
|
|
* @throws exception\SocialException |
63
|
|
|
*/ |
64
|
|
|
public function post($post){ |
65
|
|
|
$references = []; |
66
|
|
|
foreach ($this->adapters as $adapterName => $adapter){ |
67
|
|
|
$references[$adapterName] = $adapter->post($post); |
68
|
|
|
} |
69
|
|
|
return $references; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Post specified post to specified social platforms |
74
|
|
|
* |
75
|
|
|
* @param Post $post |
76
|
|
|
* @param string $adapterName |
77
|
|
|
* @param array $config |
78
|
|
|
* @return string post reference |
79
|
|
|
* @throws exception\SocialException |
80
|
|
|
*/ |
81
|
|
|
public static function postTo($post, $adapterName, $config){ |
82
|
|
|
$ms = new self([$adapterName => $config]); |
83
|
|
|
$refs = $ms->post($post); |
84
|
|
|
return array_shift($refs); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |