Passed
Push — develop ( fefadb...6c2c37 )
by Martin
32s
created

AllInOne::canPublish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\SocialPost\Provider;
6
7
use MartinGeorgiev\SocialPost\Provider\FailureWhenPublishingMessage;
8
use Throwable;
9
10
/**
11
 * Publish simultaneously to all configured social networks
12
 *
13
 * @since 1.0.0
14
 * @author Martin Georgiev <[email protected]>
15
 * @license https://opensource.org/licenses/MIT MIT
16
 * @link https://github.com/martin-georgiev/social-post-bundle Package's homepage
17
 */
18
class AllInOne implements SocialNetworkPublisher
19
{
20
    /**
21
     * @var array
22
     */
23
    private $publishers = [];
24
25
    /**
26
     * @param SocialNetworkPublisher ...$publishers List of instantiated SocialNetworkPublisher's
27
     */
28
    public function __construct(SocialNetworkPublisher ...$publishers)
29
    {
30
        foreach ($publishers as $publisher) {
31
            $this->publishers[] = $publisher;
32
        }
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function canPublish(Message $message): bool
39
    {
40
        return true;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function publish(Message $message): bool
47
    {
48
        try {
49
            $allPublished = (int)true;
50
            foreach ($this->publishers as $publisher) {
51
                if ($publisher->canPublish($message)) {
52
                    $allPublished &= $publisher->publish($message);
53
                }
54
            }
55
56
            return (bool)$allPublished;
57
        } catch (Throwable $t) {
58
            throw new FailureWhenPublishingMessage($t);
59
        }
60
    }
61
}
62