1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Writer; |
4
|
|
|
|
5
|
|
|
use Actualys\Bundle\DrupalCommerceConnectorBundle\Item\DrupalItemStep; |
6
|
|
|
use Akeneo\Bundle\BatchBundle\Item\ItemWriterInterface; |
7
|
|
|
use Akeneo\Bundle\BatchBundle\Event\InvalidItemEvent; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
9
|
|
|
use Akeneo\Bundle\BatchBundle\Event\EventInterface; |
10
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
View Code Duplication |
class FamilyWriter extends DrupalItemStep implements ItemWriterInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected $eventDispatcher; |
17
|
|
|
|
18
|
|
|
public function __construct(EventDispatcher $eventDispatcher) |
19
|
|
|
{ |
20
|
|
|
$this->eventDispatcher = $eventDispatcher; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $items |
25
|
|
|
*/ |
26
|
|
|
public function write(array $items) |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
//file_put_contents('families.json', json_encode($items)); |
30
|
|
|
foreach ($items as $item) { |
31
|
|
|
try { |
32
|
|
|
$this->webservice->sendFamily($item); |
33
|
|
|
} catch (\Exception $e) { |
34
|
|
|
$event = new InvalidItemEvent( |
35
|
|
|
__CLASS__, |
36
|
|
|
$e->getMessage(), |
37
|
|
|
array(), |
38
|
|
|
['code' => array_key_exists('code', $item) ? $item['code'] : 'NULL'] |
39
|
|
|
); |
40
|
|
|
// Logging file |
41
|
|
|
$this->eventDispatcher->dispatch( |
42
|
|
|
EventInterface::INVALID_ITEM, |
43
|
|
|
$event |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
// Loggin Interface |
48
|
|
|
$this->stepExecution->addWarning( |
49
|
|
|
__CLASS__, |
50
|
|
|
$e->getMessage(), |
51
|
|
|
array(), |
52
|
|
|
['code' => array_key_exists('code', $item) ? $item['code'] : 'NULL'] |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
/** @var ClientErrorResponseException $e */ |
56
|
|
|
if ($e->getResponse()->getStatusCode() <= 404) { |
57
|
|
|
$e = new \Exception($e->getResponse()->getReasonPhrase()); |
58
|
|
|
$this->stepExecution->addFailureException($e); |
59
|
|
|
$exitStatus = new ExitStatus(ExitStatus::FAILED); |
60
|
|
|
$this->stepExecution->setExitStatus($exitStatus); |
61
|
|
|
} |
62
|
|
|
// Handle next element. |
63
|
|
|
} |
64
|
|
|
$this->stepExecution->incrementSummaryInfo('write'); |
65
|
|
|
$this->stepExecution->incrementWriteCount(); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.