ProductProcessor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
c 2
b 1
f 0
lcom 1
cbo 4
dl 0
loc 131
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A process() 0 7 1
B normalizeProduct() 0 32 2
A getConfigurationFields() 0 17 1
A getChannel() 0 4 1
A setChannel() 0 4 1
1
<?php
2
3
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Processor;
4
5
use Akeneo\Bundle\BatchBundle\Entity\StepExecution;
6
use Akeneo\Bundle\BatchBundle\Item\InvalidItemException;
7
use Akeneo\Bundle\BatchBundle\Item\ItemProcessorInterface;
8
use Pim\Bundle\CatalogBundle\Manager\ProductManager;
9
use Pim\Bundle\CatalogBundle\Manager\ChannelManager;
10
use Pim\Bundle\CatalogBundle\Entity\Channel;
11
use Pim\Bundle\CatalogBundle\Model\ProductInterface;
12
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Exception\NormalizeException;
13
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\ProductNormalizer;
14
use Actualys\Bundle\DrupalCommerceConnectorBundle\Item\DrupalItemStep;
15
16
class ProductProcessor extends DrupalItemStep implements ItemProcessorInterface
17
{
18
    /** @var StepExecution */
19
    protected $stepExecution;
20
21
    /** @var ProductManager */
22
    protected $productManager;
23
24
    /** @var  ChannelManager */
25
    protected $channelManager;
26
27
    /** @var  array */
28
    protected $globalContext;
29
30
    /** @var  ProductNormalizer */
31
    protected $productNormalizer;
32
33
    /** @var  Channel $channel */
34
    protected $channel;
35
36
37
    /**
38
     * @param ProductManager    $productManager
39
     * @param ChannelManager    $channelManager
40
     * @param ProductNormalizer $productNormalizer
41
     */
42
    public function __construct(
43
      ProductManager $productManager,
44
      ChannelManager $channelManager,
45
      ProductNormalizer $productNormalizer
46
    ) {
47
        $this->productManager    = $productManager;
48
        $this->channelManager    = $channelManager;
49
        $this->productNormalizer = $productNormalizer;
50
    }
51
52
    /**
53
     * @param  mixed                $product
54
     * @return array|mixed
55
     * @throws InvalidItemException
56
     */
57
    public function process($product)
58
    {
59
        /** @var Channel $channel */
60
        $channel = $this->channelManager->getChannelByCode($this->channel);
61
62
        return $this->normalizeProduct($product, $channel);
63
    }
64
65
    /**
66
     * Normalize the given product
67
     *
68
     * @param ProductInterface $product [description]
69
     * @param Channel          $channel
70
     *
71
     * @throws InvalidItemException If a normalization error occurs
72
     *
73
     * @return array processed item
74
     */
75
    protected function normalizeProduct(
76
      ProductInterface $product,
77
      Channel $channel
78
    ) {
79
        try {
80
            $processedItem = $this->productNormalizer->normalize(
81
              $product,
82
              null,
83
              [
84
                'channel'       => $channel,
85
                'configuration' => $this->getConfiguration()
86
              ]
87
            );
88
        } catch (NormalizeException $e) {
89
            throw new InvalidItemException(
90
              $e->getMessage(),
91
              [
92
                'id'                                                 => $product->getId(
93
                ),
94
                $product->getIdentifier()->getAttribute()->getCode(
95
                )                                                    => $product->getIdentifier(
96
                )->getData(),
97
                'label'                                              => $product->getLabel(
98
                ),
99
                'family'                                             => $product->getFamily(
100
                )->getCode()
101
              ]
102
            );
103
        }
104
105
        return $processedItem;
106
    }
107
108
    /**
109
     * Get fields for the twig
110
     *
111
     * @return array
112
     */
113
    public function getConfigurationFields()
114
    {
115
        return array_merge(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(paren...ort.channel.label')))); (array<string,array>) is incompatible with the return type of the parent method Actualys\Bundle\DrupalCo...:getConfigurationFields of type array<string,array<strin...tring,boolean|string>>>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
116
          parent::getConfigurationFields(),
117
          [
118
            'channel'     => [
119
              'type'    => 'choice',
120
              'options' => [
121
                'choices'  => $this->channelManager->getChannelChoices(),
122
                'required' => true,
123
                'help'     => 'actualys_drupal_commerce_connector.export.channel.help',
124
                'label'    => 'actualys_drupal_commerce_connector.export.channel.label',
125
              ],
126
            ],
127
          ]
128
        );
129
    }
130
131
    /**
132
     * @return Channel
133
     */
134
    public function getChannel()
135
    {
136
        return $this->channel;
137
    }
138
139
    /**
140
     * @param Channel $channel
141
     */
142
    public function setChannel($channel)
143
    {
144
        $this->channel = $channel;
145
    }
146
}
147