QueueableEntityFormatter::canFormat()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
namespace Workana\AsyncJobs\Formatter;
3
4
use Workana\AsyncJobs\Util\ClassUtils;
5
use Workana\AsyncJobs\Doctrine\QueueableEntityInterface;
6
7
/**
8
 * @author Carlos Frutos <[email protected]>
9
 */
10
class QueueableEntityFormatter extends AggregateFormatterAware
11
{
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function format($target)
17
    {
18
        $actualClass = current(array_reverse(explode('\\', ClassUtils::getRealClass($target))));
19
20
        return strtr('Entity#:entityClass with key = :id', [
21
            ':entityClass' => $actualClass,
22
            ':id' => $this->formatId($target),
23
        ]);
24
    }
25
26
    /**
27
     * @param QueueableEntityInterface $target
28
     * @return string
29
     */
30
    protected function formatId(QueueableEntityInterface $target)
31
    {
32
        $id = $target->getId();
33
34
        if (is_array($id)) {
35
            $joinedArray = implode(', ', $id);
36
37
            return "[{$joinedArray}]";
38
        }  {
39
            return (string) $id;
40
        }
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function canFormat($target)
47
    {
48
        if (!is_object($target)) {
49
            return false;
50
        }
51
52
        return is_object($target) && is_a(ClassUtils::getRealClass($target), QueueableEntityInterface::class, true);
53
    }
54
}