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

MultiSocial::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
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
}