GroupWriter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 7
c 6
b 1
f 0
lcom 1
cbo 6
dl 55
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
B write() 42 42 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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