GroupWriter::write()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 42
Code Lines 25

Duplication

Lines 42
Ratio 100 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 42
loc 42
rs 8.439
cc 6
eloc 25
nc 4
nop 1
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 View Code Duplication
class GroupWriter extends DrupalItemStep implements ItemWriterInterface
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    protected $eventDispatcher;
15
16
    public function __construct(EventDispatcher $eventDispatcher)
17
    {
18
        $this->eventDispatcher = $eventDispatcher;
19
    }
20
21
    /**
22
     * @param array $items
23
     */
24
    public function write(array $items)
25
    {
26
        //file_put_contents('group.json', json_encode($items));
27
        foreach ($items as $item) {
28
            try {
29
                $this->webservice->sendGroup($item);
30
            } catch (\Exception $e) {
31
                $event = new InvalidItemEvent(
32
                  __CLASS__,
33
                  $e->getMessage(),
34
                  array(),
35
                  ['code' => array_key_exists('code', $item) ? $item['code'] : 'NULL']
36
                );
37
                // Logging file
38
                $this->eventDispatcher->dispatch(
39
                  EventInterface::INVALID_ITEM,
40
                  $event
41
                );
42
43
44
                // Loggin Interface
45
                $this->stepExecution->addWarning(
46
                  __CLASS__,
47
                  $e->getMessage(),
48
                  array(),
49
                  ['code' => array_key_exists('code', $item) ? $item['code'] : 'NULL']
50
                );
51
52
                /** @var ClientErrorResponseException  $e */
53
                if ($e->getResponse()->getStatusCode() <= 404) {
54
                    $e = new \Exception($e->getResponse()->getReasonPhrase());
55
                    $this->stepExecution->addFailureException($e);
56
                    $exitStatus = new ExitStatus(ExitStatus::FAILED);
57
                    $this->stepExecution->setExitStatus($exitStatus);
58
                }
59
                // Handle next element.
60
            }
61
            $this->stepExecution->incrementSummaryInfo('write');
62
            $this->stepExecution->incrementWriteCount();
63
64
        }
65
    }
66
}
67