Completed
Pull Request — master (#506)
by Paweł
21:46 queued 11:28
created

CompositeOutputChannelAdapter::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2018 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Adapter;
18
19
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
20
use SWP\Bundle\CoreBundle\Model\OutputChannelInterface;
21
22
final class CompositeOutputChannelAdapter implements AdapterInterface
23
{
24
    /**
25
     * @var array
26
     */
27
    private $adapters = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function create(OutputChannelInterface $outputChannel, ArticleInterface $article): void
33
    {
34
        $adapter = $this->getSupportedAdapter($outputChannel);
35
        $adapter->create($outputChannel, $article);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function update(OutputChannelInterface $outputChannel, ArticleInterface $article): void
42
    {
43
        $adapter = $this->getSupportedAdapter($outputChannel);
44
        $adapter->update($outputChannel, $article);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function publish(OutputChannelInterface $outputChannel, ArticleInterface $article): void
51
    {
52
        $adapter = $this->getSupportedAdapter($outputChannel);
53
        $adapter->publish($outputChannel, $article);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function unpublish(OutputChannelInterface $outputChannel, ArticleInterface $article): void
60
    {
61
        $adapter = $this->getSupportedAdapter($outputChannel);
62
        $adapter->unpublish($outputChannel, $article);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function supports(OutputChannelInterface $outputChannel): bool
69
    {
70
        foreach ($this->adapters as $adapter) {
71
            if ($adapter->supports($outputChannel)) {
72
                return true;
73
            }
74
        }
75
76
        return false;
77
    }
78
79
    /**
80
     * @param AdapterInterface $provider
81
     */
82
    public function addAdapter(AdapterInterface $provider): void
83
    {
84
        $this->adapters[] = $provider;
85
    }
86
87
    /**
88
     * @param OutputChannelInterface $outputChannel
89
     *
90
     * @throws \InvalidArgumentException
91
     *
92
     * @return AdapterInterface
93
     */
94
    private function getSupportedAdapter(OutputChannelInterface $outputChannel): AdapterInterface
95
    {
96
        foreach ($this->adapters as $adapter) {
97
            if ($adapter->supports($outputChannel)) {
98
                return $adapter;
99
            }
100
        }
101
102
        throw new \InvalidArgumentException(sprintf('There is no adapter provider registered which supports %s type!', $outputChannel->getType()));
103
    }
104
}
105