Label::updateListLabels()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 18
cp 0
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 11
nc 12
nop 1
crap 42
1
<?php
2
3
/**
4
 * AnimeDb package.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\CatalogBundle\Repository;
12
13
use AnimeDb\Bundle\CatalogBundle\Entity\Item as ItemEntity;
14
use Doctrine\ORM\EntityRepository;
15
use Doctrine\Common\Collections\ArrayCollection;
16
17
/**
18
 * @author  Peter Gribanov <[email protected]>
19
 */
20
class Label extends EntityRepository
21
{
22
    /**
23
     * @param ArrayCollection $new_labels
24
     */
25
    public function updateListLabels(ArrayCollection $new_labels)
26
    {
27
        $old_label = new ArrayCollection($this->findAll());
28
        // remove labels
29
        foreach ($old_label as $label) {
30
            if (!$new_labels->contains($label)) {
31
                foreach ($label->getItems() as $item) {
32
                    /* @var $item ItemEntity */
33
                    $item->removeLabel($label);
34
                }
35
                $this->getEntityManager()->remove($label);
36
            }
37
        }
38
39
        // add new labals
40
        foreach ($new_labels as $label) {
41
            if (!$old_label->contains($label)) {
42
                $this->getEntityManager()->persist($label);
43
            }
44
        }
45
46
        $this->getEntityManager()->flush();
47
    }
48
}
49