Completed
Push — wip-subtree ( 04b705...405079 )
by
unknown
12:15
created

CollectionsManager::preUpdateCollectionsManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\Bundle\CoreBundle\Admin\Traits;
14
15
use Doctrine\ORM\Mapping\ClassMetadataInfo;
16
17
trait CollectionsManager
18
{
19
    protected $managedCollections = [];
20
21
    /**
22
     * function getManagedCollections.
23
     *
24
     * @return array
25
     * */
26
    public function getManagedCollections()
27
    {
28
        return $this->managedCollections;
29
    }
30
31
    /**
32
     * function addManagedCollections.
33
     *
34
     * @param $collections      array or string, describing the collections to manage
35
     *
36
     * @return CoreAdmin $this
37
     * */
38
    public function addManagedCollections($collections)
39
    {
40
        if (!is_array($collections)) {
41
            $collections = array($collections);
42
        }
43
        $this->managedCollections = array_merge($this->managedCollections, $collections);
44
45
        return $this;
46
    }
47
48
    public function prePersistCollectionsManager($object)
49
    {
50
        $this->preUpdateOrPersistCollectionsManager($object);
51
    }
52
53
    public function preUpdateCollectionsManager($object)
54
    {
55
        $this->preUpdateOrPersistCollectionsManager($object);
56
    }
57
58
    protected function preUpdateOrPersistCollectionsManager($object)
59
    {
60
        // global configuration
61
        $this->configureCollectionsManager();
62
63
        // for each given collection
64
        foreach ($this->managedCollections as $coll) {
65
            if (isset($this->formFieldDescriptions[$coll])) {
66
                if ($coll == 'packagings') {
67
                    dump($this->formFieldDescriptions[$coll]);
0 ignored issues
show
Bug introduced by
The property formFieldDescriptions 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...
68
                }
69
                // preparing stuff
70
                if ($admin_code = $this->formFieldDescriptions[$coll]->getOption('admin_code')) {
71
                    $targetAdmin = $this->getConfigurationPool()->getAdminByAdminCode($admin_code);
0 ignored issues
show
Bug introduced by
It seems like getConfigurationPool() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
                } else {
73
                    $target = $this->getModelManager()
0 ignored issues
show
Bug introduced by
It seems like getModelManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
                    ->getEntityManager($object)
75
                    ->getClassMetadata($this->getClass())
0 ignored issues
show
Bug introduced by
It seems like getClass() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
                    ->associationMappings[$coll]['targetEntity']
77
                ;
78
79
                    $rctarget = new \ReflectionClass($target);
80
                    $targetAdmin = $this->getConfigurationPool()->getAdminByClass($rctarget->getName());
0 ignored issues
show
Bug introduced by
It seems like getConfigurationPool() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
Consider using $rctarget->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
81
                }
82
83
                $rcentity = new \ReflectionClass($this->getClass());
0 ignored issues
show
Bug introduced by
It seems like getClass() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
84
                $method = 'get' . ucfirst($coll);
85
86
                $subObjects = $object->$method();
87
88
                // insert/update (forcing the foreign key to be set to $this->getId(), for instance)
89
                if ($subObjects != null) {
90
                    foreach ($subObjects as $subobj) {
91
                        if ($this->formFieldDescriptions[$coll]->getMappingType() != ClassMetadataInfo::MANY_TO_MANY) {
92
                            $subobj->{'set' . ucfirst($rcentity->getShortName())}($object);
93
                        }
94
95
                        if ($targetAdmin != null) {
96
                            $targetAdmin->prePersist($subobj);
97
                        }
98
                    }
99
                }
100
101
                if (!$subObjects instanceof Doctrine\ORM\PersitentCollection || $subObjects->count() == 0) {
0 ignored issues
show
Bug introduced by
The class Blast\Bundle\CoreBundle\...ORM\PersitentCollection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
102
                    continue;
103
                }
104
105
                // delete
106
                foreach ($subObjects->getSnapshot() as $subobj) {
107
                    if (!$subObjects->contains($subobj)) {
108
                        $this->getModelManager()->delete($subobj);
0 ignored issues
show
Bug introduced by
It seems like getModelManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
109
                    }
110
                }
111
            }
112
        }
113
114
        return $this;
115
    }
116
117 View Code Duplication
    private function configureCollectionsManager()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $blast = $this->getConfigurationPool()->getContainer()->getParameter('blast');
0 ignored issues
show
Bug introduced by
It seems like getConfigurationPool() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
120
        $key = 'collections'; // name of the key in the blast.yml
121
        // merge configuration/parameters
122
        foreach ($this->getCurrentComposition() as $class) {
0 ignored issues
show
Bug introduced by
It seems like getCurrentComposition() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
123
            if (isset($blast[$class]) && isset($blast[$class]['manage']) && isset($blast[$class]['manage'][$key])) {
124
                if (!is_array($blast[$class]['manage'][$key])) {
125
                    $blast[$class]['manage'][$key] = [$blast[$class]['manage'][$key]];
126
                }
127
                $this->addManagedCollections($blast[$class]['manage'][$key]);
128
            }
129
        }
130
131
        return $this;
132
    }
133
}
134