AllInOne::publish()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\SocialPost\SocialNetwork;
6
7
use MartinGeorgiev\SocialPost\Message;
8
use MartinGeorgiev\SocialPost\Publisher;
9
use MartinGeorgiev\SocialPost\SocialNetwork\Exception\FailureWhenPublishingMessage;
10
11
/**
12
 * Publish simultaneously to all configured social networks
13
 *
14
 * @license https://opensource.org/licenses/MIT
15
 * @link https://github.com/martin-georgiev/social-post
16
 */
17
class AllInOne implements Publisher
18
{
19
    /**
20
     * @var Publisher[]
21
     */
22
    private $publishers = [];
23
24
    public function __construct(Publisher ...$publishers)
25
    {
26
        foreach ($publishers as $publisher) {
27
            $this->publishers[] = $publisher;
28
        }
29
    }
30
31
    public function canPublish(Message $message): bool
32
    {
33
        return true;
34
    }
35
36
    public function publish(Message $message): bool
37
    {
38
        try {
39
            $allPublished = true;
40
            foreach ($this->publishers as $publisher) {
41
                if ($publisher->canPublish($message)) {
42
                    $allPublished &= $publisher->publish($message);
43
                }
44
            }
45
46
            return (bool) $allPublished;
47
        } catch (\Exception $e) {
48
            throw new FailureWhenPublishingMessage($e);
49
        }
50
    }
51
}
52