ItemExportExecutor::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace TreeHouse\IoBundle\Bridge\WorkerBundle\Executor;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use TreeHouse\IoBundle\Export\FeedExporter;
10
use TreeHouse\WorkerBundle\Executor\AbstractExecutor;
11
use TreeHouse\WorkerBundle\Executor\ObjectPayloadInterface;
12
13
class ItemExportExecutor extends AbstractExecutor implements ObjectPayloadInterface
14
{
15
    const NAME = 'item.export';
16
17
    /**
18
     * @var FeedExporter
19
     */
20
    protected $exporter;
21
22
    /**
23
     * @var ManagerRegistry
24
     */
25
    protected $doctrine;
26
27
    /**
28
     * @param FeedExporter    $exporter
29
     * @param ManagerRegistry $doctrine
30
     */
31
    public function __construct(FeedExporter $exporter, ManagerRegistry $doctrine)
32
    {
33
        $this->exporter = $exporter;
34
        $this->doctrine = $doctrine;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function getName()
41
    {
42
        return self::NAME;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function supportsObject($object)
49
    {
50
        return $this->exporter->supports($object);
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 View Code Duplication
    public function getObjectPayload($object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        $class = get_class($object);
59
        $meta = $this->doctrine->getManagerForClass($class)->getClassMetadata($class);
60
61
        return [$class, $meta->getIdentifierValues($object)];
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function configurePayload(OptionsResolver $resolver)
68
    {
69
        $resolver->setRequired(0);
70
        $resolver->setRequired(1);
71
        $resolver->setAllowedTypes(0, 'string');
72
        $resolver->setAllowedTypes(1, 'array');
73
        $resolver->setNormalizer(1, function (Options $options, $value) {
74
            $class = $options[0];
75
            if (null === $item = $this->doctrine->getRepository($class)->findOneBy($value)) {
76
                throw new InvalidArgumentException(sprintf('Could not find %s with id %d', $class, $value));
77
            }
78
79
            return $item;
80
        });
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function execute(array $payload)
87
    {
88
        $item = $payload[1];
89
90
        return $this->exporter->cacheItem($item, [], true);
91
    }
92
}
93