ItemExportRemoveExecutor::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 Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\OptionsResolver\Options;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use TreeHouse\IoBundle\Export\FeedExporter;
11
use TreeHouse\WorkerBundle\Executor\AbstractExecutor;
12
use TreeHouse\WorkerBundle\Executor\ObjectPayloadInterface;
13
14
class ItemExportRemoveExecutor extends AbstractExecutor implements ObjectPayloadInterface
15
{
16
    const NAME = 'item.export.remove';
17
18
    /**
19
     * @var FeedExporter
20
     */
21
    protected $exporter;
22
23
    /**
24
     * @var ManagerRegistry
25
     */
26
    protected $doctrine;
27
28
    /**
29
     * @param FeedExporter    $exporter
30
     * @param ManagerRegistry $doctrine
31
     */
32
    public function __construct(FeedExporter $exporter, ManagerRegistry $doctrine)
33
    {
34
        $this->exporter = $exporter;
35
        $this->doctrine = $doctrine;
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function getName()
42
    {
43
        return self::NAME;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function supportsObject($object)
50
    {
51
        return $this->exporter->supports($object);
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 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...
58
    {
59
        $class = get_class($object);
60
        $meta = $this->doctrine->getManagerForClass($class)->getClassMetadata($class);
61
62
        return [$class, $meta->getIdentifierValues($object)];
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function configurePayload(OptionsResolver $resolver)
69
    {
70
        $resolver->setRequired(0);
71
        $resolver->setRequired(1);
72
        $resolver->setAllowedTypes(0, 'string');
73
        $resolver->setAllowedTypes(1, 'array');
74
        $resolver->setNormalizer(1, function (Options $options, $value) {
75
            $class = $options[0];
76
77
            // use a reference if the item does not exist anymore:
78
            // maybe we're cleaning up after it's been removed
79
            if (null === $item = $this->doctrine->getRepository($class)->findOneBy($value)) {
80
                /** @var EntityManagerInterface $manager */
81
                $manager = $this->doctrine->getManagerForClass($class);
82
                $item = $manager->getReference($class, $value);
83
            }
84
85
            return $item;
86
        });
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function execute(array $payload)
93
    {
94
        $item = $payload[1];
95
96
        // remove export directory
97
        foreach ($this->exporter->getTypes() as $type) {
98
            if ($type->supports($item)) {
99
                $dir = dirname($this->exporter->getItemCacheFilename($item, $type));
100
                (new Filesystem())->remove($dir);
101
            }
102
        }
103
104
        return true;
105
    }
106
}
107