MessageFactory::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the LineMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LineMob\Core\Factory;
13
14
use LineMob\Core\Command\AbstractCommand;
15
use LineMob\Core\Message\MessageInterface;
16
use LineMob\Core\Template\TemplateInterface;
17
18
/**
19
 * @author Ishmael Doss <[email protected]>
20
 */
21
class MessageFactory implements MessageFactoryInterface
22
{
23
    /**
24
     * @var MessageInterface[]
25
     */
26
    private $messages;
27
28
    public function __construct(array $messages = [])
29
    {
30
        $this->messages = $messages;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function add(MessageInterface $message)
37
    {
38
        $this->messages[] = $message;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function createMessage(AbstractCommand $command)
45
    {
46
        foreach ($this->messages as $message) {
47
            if ($message->supported($command)) {
48
                return $message->createTemplate($command->message);
49
            }
50
        }
51
52
        if ($command->message instanceof TemplateInterface) {
53
            $type = get_class($command->message);
54
        } else {
55
            $type = strval($command->message);
56
        }
57
58
        throw new \InvalidArgumentException(
59
            sprintf("Unsupported message type: `%s` for `%s`.", $type, get_class($command))
60
        );
61
    }
62
}
63