AttributeOptionWriter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 5
c 5
b 1
f 0
lcom 1
cbo 5
dl 0
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B write() 0 42 4
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 Akeneo\Bundle\BatchBundle\Job\ExitStatus;
11
12
/**
13
 * Class AttributeOptionWriter
14
 * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Writer
15
 */
16
class AttributeOptionWriter extends DrupalItemStep implements ItemWriterInterface
17
{
18
    /**
19
     * @var EventDispatcher
20
     */
21
    protected $eventDispatcher;
22
23
    /**
24
     * @param EventDispatcher $eventDispatcher
25
     */
26
    public function __construct(EventDispatcher $eventDispatcher)
27
    {
28
        $this->eventDispatcher = $eventDispatcher;
29
    }
30
31
    /**
32
     * @param array $items
33
     */
34
    public function write(array $items)
35
    {
36
        //      file_put_contents('options.json', json_encode($items));
37
        foreach ($items as $item) {
38
            try {
39
                $this->webservice->sendAttributeOption($item);
40
41
            } catch (\Exception $e) {
42
                $event = new InvalidItemEvent(
43
                  __CLASS__,
44
                  $e->getMessage(),
45
                  array(),
46
                  ['code' => key($item)]
47
                );
48
                // Logging file
49
                $this->eventDispatcher->dispatch(
50
                  EventInterface::INVALID_ITEM,
51
                  $event
52
                );
53
54
55
                // Loggin Interface
56
                $this->stepExecution->addWarning(
57
                  __CLASS__,
58
                  $e->getMessage(),
59
                  array(),
60
                  ['code' => key($item) ]
61
                );
62
63
                /** @var ClientErrorResponseException  $e */
64
                if ($e->getResponse()->getStatusCode() <= 404) {
65
                    $e = new \Exception($e->getResponse()->getReasonPhrase());
66
                    $this->stepExecution->addFailureException($e);
67
                    $exitStatus = new ExitStatus(ExitStatus::FAILED);
68
                    $this->stepExecution->setExitStatus($exitStatus);
69
                }
70
                // Handle next element.
71
            }
72
            $this->stepExecution->incrementWriteCount();
73
            $this->stepExecution->incrementSummaryInfo('write');
74
        }
75
    }
76
}
77