ProductWriter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 8
c 8
b 1
f 1
lcom 1
cbo 7
dl 66
loc 66
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A getConfigurationFields() 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 Akeneo\Bundle\BatchBundle\Job\ExitStatus;
9
use Guzzle\Http\Exception\ClientErrorResponseException;
10
use Symfony\Component\EventDispatcher\EventDispatcher;
11
use Akeneo\Bundle\BatchBundle\Event\EventInterface;
12
13
/**
14
 * Class ProductWriter
15
 *
16
 * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Writer
17
 */
18 View Code Duplication
class ProductWriter 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...
19
{
20
21
    public function __construct(EventDispatcher $eventDispatcher)
22
    {
23
        $this->eventDispatcher = $eventDispatcher;
0 ignored issues
show
Bug introduced by
The property eventDispatcher does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $this->productandler   = $eventDispatcher;
0 ignored issues
show
Bug introduced by
The property productandler does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    }
26
27
    /**x
28
     *
29
     * @return array
30
     */
31
    public function getConfigurationFields()
32
    {
33
        return parent::getConfigurationFields();
34
    }
35
36
    /**
37
     * @param array $items
38
     */
39
    public function write(array $items)
40
    {
41
        foreach ($items as $item) {
42
            try {
43
          //file_put_contents('product.json', json_encode($item));
44
              $this->webservice->sendProduct($item);
45
            } catch (\Exception $e) {
46
                $event = new InvalidItemEvent(
47
                  __CLASS__,
48
                  $e->getMessage(),
49
                  array(),
50
                  ['sku' => array_key_exists('sku', $item) ? $item['sku'] : 'NULL']
51
                );
52
                // Logging file
53
                $this->eventDispatcher->dispatch(
54
                  EventInterface::INVALID_ITEM,
55
                  $event
56
                );
57
58
59
                // Loggin Interface
60
                $this->stepExecution->addWarning(
61
                  __CLASS__,
62
                  $e->getMessage(),
63
                  array(),
64
                  ['sku' => array_key_exists('sku', $item) ? $item['sku'] : 'NULL']
65
                );
66
67
                /** @var ClientErrorResponseException  $e */
68
                if ($e->getResponse()->getStatusCode() <= 404) {
69
                    $e = new \Exception($e->getResponse()->getReasonPhrase());
70
                    $this->stepExecution->addFailureException($e);
71
                    $exitStatus = new ExitStatus(ExitStatus::FAILED);
72
                    $this->stepExecution->setExitStatus($exitStatus);
73
                }
74
                // Handle next element.
75
            }
76
            $this->stepExecution->incrementSummaryInfo('write');
77
            $this->stepExecution->incrementWriteCount();
78
79
        }
80
    }
81
82
83
}
84