ManagerArrayTrait::getModelInfos()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Rafidion Michael
5
 * Date: 10/12/2014
6
 * Time: 12:34
7
 */
8
9
namespace Mykees\TagBundle\Traits;
10
11
12
use Mykees\TagBundle\Entity\Tag;
13
use Mykees\TagBundle\Interfaces\Taggable;
14
15
trait ManagerArrayTrait {
16
17
    /**
18
     * Return all Tags for an array of objects
19
     * @param array $models
20
     * @return mixed
21
     */
22
    protected function getTagRelationArray( array $models )
23
    {
24
        $model_info = $this->getModelInfos($models);
25
26
        $query = $this->em->createQueryBuilder()
0 ignored issues
show
Bug introduced by
The property em 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...
27
            ->select('tr')
28
            ->from($this->tagRelation,'tr')
0 ignored issues
show
Bug introduced by
The property tagRelation 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...
29
            ->innerJoin('tr.tag','t')
30
            ->addSelect('t')
31
            ->where('tr.model IN(:model)')
32
            ->andWhere('tr.modelId IN(:modelId)')
33
            ->setParameter('model',array_values($model_info['models']))
34
            ->setParameter('modelId',array_values($model_info['ids']))
35
            ->getQuery()
36
            ->getResult()
37
        ;
38
39
        return $this->refreshTagsArray($query,$models);
40
    }
41
42
    /**
43
     * Insert tags in model referer
44
     * @param array $tagRelations
45
     * @param array $models
46
     */
47
    protected function refreshTagsArray(array $tagRelations, array $models)
48
    {
49
        $this->clean($models);
50
        foreach($models as $model)
51
        {
52
            foreach($tagRelations as $tr)
53
            {
54
                if( $model instanceof Taggable && $tr->getTag() instanceof Tag )
55
                {
56
                    if($model->getId() == $tr->getModelId() && $model->getModel() == $tr->getModel())
57
                    {
58
                        $model->getTags()->add($tr->getTag());
59
                    }
60
                }
61
            }
62
        }
63
    }
64
65
    /**
66
     * Return an array contain ids and models names
67
     * @param array $datas
68
     * @return array
69
     */
70
    protected function getModelInfos(array $datas)
71
    {
72
        $ids = [];$models = [];$model_exist=false;
73
        foreach( $datas as $k=>$data )
74
        {
75
            if($data instanceof Taggable)
76
            {
77
                array_push($ids,$data->getModelId());
78
                if( $model_exist != $data->getModel() )
79
                {
80
                    array_push($models,$data->getModel());
81
                    $model_exist = $data->getModel();
82
                }
83
            }
84
        }
85
86
        return [ 'ids'=>$ids, 'models'=>$models ];
87
    }
88
89
    /**
90
     * Clean a collection
91
     * @param array $models
92
     */
93
    protected function clean(array $models)
94
    {
95
        foreach($models as $model)
96
        {
97
            $model->getTags()->clear();
98
        }
99
    }
100
}
101