Completed
Push — master ( 9d56e7...43a437 )
by Prabath
10:19 queued 08:51
created

MultiSocial   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 76
ccs 9
cts 18
cp 0.5
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapter() 0 3 2
A post() 0 6 2
A __construct() 0 7 2
A postTo() 0 4 1
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
}