SonataAdminClientController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 8
dl 0
loc 82
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A preCreate() 0 6 1
A preEdit() 0 6 1
C updateContacts() 0 55 10
1
<?php
2
3
namespace ClientBundle\Controller;
4
5
use Sonata\AdminBundle\Controller\CRUDController;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
9
/**
10
 * Class SonataAdminClientController
11
 */
12
class SonataAdminClientController extends CRUDController
13
{
14
    /**
15
     * @param Request $request
16
     * @param mixed   $object
17
     * @return null|Response
18
     */
19
    protected function preCreate(Request $request, $object)
20
    {
21
        parent::preCreate($request, $object);
22
23
        return $this->updateContacts($request, $object);
24
    }
25
26
    /**
27
     * @param Request $request
28
     * @param mixed   $object
29
     * @return null|Response
30
     */
31
    protected function preEdit(Request $request, $object)
32
    {
33
        parent::preEdit($request, $object);
34
35
        return $this->updateContacts($request, $object);
36
    }
37
38
    private function updateContacts(Request $request, $object)
39
    {
40
        $arr1 = [];
41
        //шматок гавнокода
42
        $postData = $request->request->all();
43
        foreach ($postData as $arr) {
44
            $arr1 = $arr;
45
            break;
46
        }
47
        $tmpTest = '';
48
49
        if ($postData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $postData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
            $oldContacts = null;
0 ignored issues
show
Unused Code introduced by
$oldContacts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
            $newContacts = null;
0 ignored issues
show
Unused Code introduced by
$newContacts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
            $oldContacts = $object->getContacts();
53
            $em = $this->getDoctrine()->getManager();
54
            if (array_key_exists('contacts', $arr1)) {
55
                $newContacts = $arr1['contacts'];
56
57
                // Викидуем з массива нових контактів, які прийшли з POST, ті що вже були
58
                // і видаляем з бази контакти які видалені у клієнта
59
                foreach ($oldContacts as $contact) {
60
                    $k = array_search($contact->getId(), $newContacts);
61
                    if (false === $k) {
62
                        $object->removeContact($contact);
63
                        $em->remove($contact);
64
                    } else {
65
                        unset($newContacts[$k]);
66
                    }
67
                }
68
69
                foreach ($newContacts as $contactId) {
70
                    $contact = $em->getRepository('ClientBundle:Contact')->find($contactId);
71
                    if (!$contact) {
72
                        throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $contactId));
73
                    } else {
74
                        $object->addContact($contact);
75
                        $contact->setClient($object);
76
                    }
77
                }
78
            } else {
79
                // Post прийшов без контактів - видаляем всі контакти клієнта
80
                foreach ($oldContacts as $contact) {
81
                    $object->removeContact($contact);
82
                    $em->remove($contact);
83
                }
84
            }
85
        }
86
87
        if ('' === $tmpTest) {
88
            return null;
89
        } else {
90
            return new Response($tmpTest);
91
        }
92
    }
93
}
94