AggregateFormatter::canFormat()   A
last analyzed

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
 * Created by PhpStorm.
4
 * User: cronopio
5
 * Date: 27/11/16
6
 * Time: 19:42
7
 */
8
9
namespace Workana\AsyncJobs\Formatter;
10
use Workana\AsyncJobs\Util\NormalPriorityQueue;
11
12
/**
13
 * Aggregate of formatters
14
 *
15
 * @author Carlos Frutos <[email protected]>
16
 */
17
class AggregateFormatter
18
{
19
    /**
20
     * @var NormalPriorityQueue
21
     */
22
    protected $formatters;
23
24
    public function __construct()
25
    {
26
        $this->formatters = new NormalPriorityQueue();
27
    }
28
29
    /**
30
     * Add formatter
31
     *
32
     * @param FormatterInterface $formatter
33
     * @param int $priority
34
     *
35
     * @return void
36
     */
37
    public function addFormatter(FormatterInterface $formatter, $priority = 0)
38
    {
39
        if ($formatter instanceof AggregateFormatterAware) {
40
            $formatter->setAggregateFormatter($this);
41
        }
42
43
        $this->formatters->insert($formatter, (int) $priority);
44
    }
45
46
    /**
47
     * @return FormatterInterface|null
48
     */
49
    protected function getFormatter($target)
50
    {
51
        foreach (clone $this->formatters as $formatter) {
52
            if ($formatter->canFormat($target)) {
53
                return $formatter;
54
            }
55
        }
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function canFormat($target)
62
    {
63
        return (bool) $this->getFormatter($target);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function format($target)
70
    {
71
        return $this->getFormatter($target)->format($target);
72
    }
73
}