CartSerializer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 14 2
A deserialize() 0 15 3
1
<?php
2
3
namespace AppBundle\Cart;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
7
class CartSerializer
8
{
9
    private $manager;
10
11
    public function __construct(ObjectManager $manager)
12
    {
13
        $this->manager = $manager;
14
    }
15
16
    public function serialize(Cart $cart)
17
    {
18
        $data = array(
19
            'rows' => array()
20
        );
21
22
        foreach ($cart->getRows() as $row) {
23
            $data['rows'][$row->getMeal()->getId()] = array(
24
                'quantity' => $row->getQuantity()
25
            );
26
        }
27
28
        return $data;
29
    }
30
31
    public function deserialize(array $data)
32
    {
33
        $cart = new Cart();
34
        foreach ($data['rows'] as $id => $row) {
35
            $meal = $this->manager->getRepository('AppBundle:Meal')->find($id);
36
            if (!$meal) {
37
                continue;
38
            }
39
40
            $row = new CartRow($meal, $row['quantity']);
41
            $cart->addRow($row);
42
        }
43
44
        return $cart;
45
    }
46
}
47