1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Bridge\WorkerBundle\Executor; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException; |
8
|
|
|
use Symfony\Component\OptionsResolver\Options; |
9
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
10
|
|
|
use TreeHouse\IoBundle\Entity\ImportPart; |
11
|
|
|
use TreeHouse\IoBundle\Import\Feed\TransportFactory; |
12
|
|
|
use TreeHouse\IoBundle\Import\ImportFactory; |
13
|
|
|
use TreeHouse\WorkerBundle\Executor\AbstractExecutor; |
14
|
|
|
use TreeHouse\WorkerBundle\Executor\ObjectPayloadInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Performs import of a single ImportPart. |
18
|
|
|
*/ |
19
|
|
|
class ImportPartExecutor extends AbstractExecutor implements ObjectPayloadInterface |
20
|
|
|
{ |
21
|
|
|
const NAME = 'import.part'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ManagerRegistry |
25
|
|
|
*/ |
26
|
|
|
protected $doctrine; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ImportFactory |
30
|
|
|
*/ |
31
|
|
|
protected $importFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $logger; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ManagerRegistry $doctrine |
40
|
|
|
* @param ImportFactory $importFactory |
41
|
|
|
* @param LoggerInterface $logger |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ManagerRegistry $doctrine, ImportFactory $importFactory, LoggerInterface $logger) |
44
|
|
|
{ |
45
|
|
|
$this->doctrine = $doctrine; |
46
|
|
|
$this->importFactory = $importFactory; |
47
|
|
|
$this->logger = $logger; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getName() |
54
|
|
|
{ |
55
|
|
|
return self::NAME; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function getObjectPayload($object) |
62
|
|
|
{ |
63
|
|
|
return [$object->getId()]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
|
public function supportsObject($object) |
70
|
|
|
{ |
71
|
|
|
return $object instanceof ImportPart; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function configurePayload(OptionsResolver $resolver) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$resolver->setRequired(0); |
80
|
|
|
$resolver->setAllowedTypes(0, 'numeric'); |
81
|
|
|
$resolver->setNormalizer(0, function (Options $options, $value) { |
82
|
|
|
if (null === $part = $this->findImportPart($value)) { |
83
|
|
|
throw new InvalidArgumentException(sprintf('Import part with id "%d" does not exist', $value)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $part; |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
public function execute(array $payload) |
94
|
|
|
{ |
95
|
|
|
/** @var ImportPart $part */ |
96
|
|
|
list($part) = $payload; |
97
|
|
|
|
98
|
|
|
$import = $part->getImport(); |
99
|
|
|
$feed = $import->getFeed(); |
100
|
|
|
|
101
|
|
|
$this->logger->info( |
102
|
|
|
sprintf( |
103
|
|
|
'Importing part <comment>%d</comment> of %s-feed for import "%d" for origin "%s"', |
104
|
|
|
$part->getPosition(), |
105
|
|
|
$feed->getType(), |
106
|
|
|
$import->getId(), |
107
|
|
|
$feed->getOrigin()->getTitle() |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
if ($import->isFinished()) { |
112
|
|
|
$this->logger->info(sprintf('Import %d has already finished', $import->getId())); |
113
|
|
|
|
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($part->isFinished()) { |
118
|
|
|
$this->logger->info(sprintf('Part %d has already finished', $part->getId())); |
119
|
|
|
|
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (!$this->validate($part)) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$job = $this->importFactory->createImportJob($part); |
128
|
|
|
$job->setLogger($this->logger); |
129
|
|
|
$job->run(); |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param int $partId |
136
|
|
|
* |
137
|
|
|
* @return ImportPart |
138
|
|
|
*/ |
139
|
|
|
protected function findImportPart($partId) |
140
|
|
|
{ |
141
|
|
|
return $this->doctrine->getRepository('TreeHouseIoBundle:ImportPart')->find($partId); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param ImportPart $part |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
protected function validate(ImportPart $part) |
150
|
|
|
{ |
151
|
|
|
// validate that we have a valid transport config |
152
|
|
|
try { |
153
|
|
|
TransportFactory::createTransportFromConfig($part->getTransportConfig()); |
154
|
|
|
} catch (\RuntimeException $e) { |
155
|
|
|
$part->setError($e->getMessage()); |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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.