Completed
Push — master ( 53e30c...17ea5b )
by Laurent
09:45
created

AbstractInventoryController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getInvetoryEditType() 0 26 2
A createCreateForm() 0 9 1
B getLineArticles() 0 21 6
A updateArticles() 0 14 4
1
<?php
2
/**
3
 * AbstractInventoryController Méthodes communes InventoryController.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use AppBundle\Controller\AbstractController;
18
use AppBundle\Entity\Inventory;
19
use AppBundle\Form\Type\InventoryType;
20
use AppBundle\Form\Type\InventoryEditType;
21
use AppBundle\Form\Type\InventoryEditZonesType;
22
23
use Doctrine\Common\Persistence\ObjectManager;
24
25
/**
26
 * Abstract controller.
27
 *
28
 * @category Controller
29
 */
30
class AbstractInventoryController extends AbstractController
31
{
32
    /**
33
     * Get the Inventory edit type
34
     *
35
     * @param \AppBundle\Controller\Inventory $inventory Inventaire à éditer
36
     * @return array
37
     */
38
    protected function getInvetoryEditType(Inventory $inventory)
39
    {
40
        $etm = $this->getDoctrine()->getManager();
41
        $zoneStorages = null;
42
        $settings = $etm->getRepository('AppBundle:Settings')->find(1);
43
        if ($settings->getInventoryStyle() == 'zonestorage') {
44
            $zoneStorages = $etm->getRepository('AppBundle:ZoneStorage')->findAll();
45
            $typeClass = InventoryEditZonesType::class;
46
        } else {
47
            $typeClass = InventoryEditType::class;
48
        }
49
        $editForm = $this->createForm(
50
            $typeClass,
51
            $inventory,
52
            ['action' => $this->generateUrl('inventory_update', ['id' => $inventory->getId()]),
53
            'method' => 'PUT',]
54
        );
55
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
56
57
        $return = ['editForm' => $editForm,
58
            'inventory' => $inventory,
59
            'zoneStorages' => $zoneStorages,
60
            'edit_form'   => $editForm->createView(),
61
            'delete_form' => $deleteForm->createView(),];
62
        return $return;
63
    }
64
65
    /**
66
     * Create Create form.
67
     *
68
     * @param string $route Route of action form
69
     * @return \Symfony\Component\Form\Form
70
     */
71
    protected function createCreateForm($route)
72
    {
73
        $inventory = new Inventory();
74
        return $this->createForm(
75
            InventoryType::class,
76
            $inventory,
77
            ['attr' => ['id' => 'create'], 'action' => $this->generateUrl($route), 'method' => 'PUT',]
78
        );
79
    }
80
    
81
    /**
82
     * Update Articles
83
     *
84
     * @param array $articleLine tableau
85
     * @param Inventory $inventory Inventaire traité
86
     * @param array $articles Articles actifs
0 ignored issues
show
Bug introduced by
There is no parameter named $articles. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
87
     */
88
    protected function getLineArticles(array $articleLine, Inventory $inventory)
89
    {
90
        $inventoryArticles = array();
91
        $lineOk = 0;
92
        foreach ($inventory->getArticles() as $line) {
93
            foreach ($articleLine as $key => $art) {
94
                if (!empty($articleLine) && $line->getArticle()->getName() === $art['article']) {
95
                    $art['realstock'] = $art['realstock'] + $line->getRealstock();
96
                    $articleLine[$key]['realstock'] = strval(number_format($art['realstock'], 3));
97
                    $lineOk = 1;
98
                }
99
            }
100
            if ($lineOk === 0) {
101
                $inventoryArticles['article'] = $line->getArticle()->getName();
102
                $inventoryArticles['realstock'] = $line->getRealstock();
103
                array_push($articleLine, $inventoryArticles);
104
            }
105
            $lineOk = 0;
106
        }
107
        return $articleLine;
108
    }
109
110
    public function updateArticles(array $articles, ObjectManager $etm, Inventory $inventory)
111
    {
112
        $articleLine = array();
113
        $articleLine = $this->getLineArticles($articleLine, $inventory);
114
        
115
        foreach ($articles as $article) {
116
            foreach ($articleLine as $line) {
117
                if ($article->getName() === $line['article']) {
118
                    $article->setQuantity($line['realstock']);
119
                    $etm->persist($article);
120
                }
121
            }
122
        }        
123
    }
124
}
125